I'm trying to demonstrate an integer overflow bug and its consequences by writing a small code as follows:
int main(int argc, char** argv)
{
size_t len = 0;
sscanf (argv[1], "%lu", &len);
char* buffer = malloc(len + 5);
strcpy (buffer, argv[2]);
printf("str = \'%s\'\n", buffer)
return 0;
}
A safe input to this program is like this:
./program 16 "This is a string"
Where an unsafe input to demo the integer overflow is like this:
./program 18446744073709551613 "`perl -e 'print "This is a very very large string "x20'`"
Yet to my surprise, even though the integer overflow is happening and a very small buffer is being allocated, the program does NOT produce any SEGMENTATION FAULT and the program executes fine without any problems to the end!
Can someone explain why this is ?
I'm compiling this with GCC-5.2.1
and running on 64-bit Ubuntu system.
A more complete version of the code can be viewed here.