Re: [Full-disclosure] new class of printf issue: int overflow

On Thursday 11 January 2007 03:00, Felix von Leitner wrote:
$ cat > t.c
#include <stdio.h>

int main() {
printf("%d\n",snprintf(0,0,"%*d %*d",0x40000000,1,0x40000000,1));
$ gcc -o t t.c
$ ./t
./t 17.02s user 0.03s system 99% cpu 17.161 total

the second line comes from my zsh, and as you can see running this
program took 17 seconds. top shows that the process used 1 gig of
memory while it ran. :-)

This is a pretty neat trick :)

However, it takes ~1GB of virtual memory, which in Linux land is not a
biggie if you don't write anything in it.
Out of curiosity, I made the same test on Windows (cl.exe). It took ~14s to
run, but it did not go above 300KB of memory usage.

Full-Disclosure - We believe in it.
Hosted and sponsored by Secunia -

Relevant Pages

  • Re: right padding
    ... Using gcc in mingw on a windows machine.. ... helpful things a compiler can do if you are writing library code like ... int num = 123; ... area of memory freed is the area that was allocated in the subroutine. ...
  • Re: data types
    ... Usually short is smaller than an int. ... memory per dms structure if they are 32-bit integers. ... current compiler system. ... recommend either a version of gcc,, or Visual Studio ...
  • Re: Use of Long and Long Long
    ... intuitive and much more useful (as it is now, ... smaller types very inefficient since it would have to keep masking etc. So to write an 8 bit char to memory it would have to on one of the processors... ... load memory location shifting data left 8 bits (1 clock) ... but takes maybe twice as long under gcc because long int now takes twice the memory and the processor unnecessarily emulates 64-bit arithmetic. ...
  • Re: vrp bitfield miscompilation (WAS: [Regression] snd_emu10k1 doesnt work after GCC 4.2 upgrade)
    ... snd_emu10k1 doesn't work after GCC ... (these are signed single-bit variables): ... With int these flags have values of, ... I've seen that the compile will round the memory size of a bit-fields down to the nearest byte, ...
  • Re: weird behaviour by gcc
    ... The compiler can align memory as it chooses. ... > int main ... profit) when I noticed this behaviour by gcc. ...