|
From: Dan K. <da...@ke...> - 2011-11-03 15:58:47
|
On Thu, Nov 3, 2011 at 7:48 AM, Peter Toft <pt...@li...> wrote:
> small twist to the code, then gcc -O2 -Wall finds nothing....
>
> See the comment -> run as "code -1"
>
> #include <stdio.h>
> #include <stdlib.h>
>
>
> /* Save as code.c compile "gcc -Wall -O2 o code code.c" and run as "code
> -1" */
> int main(int argc, char **argv)
> {
> int a[2],b[2],c[2],i;
>
> a[0] = 1; a[1] = 2;
> b[0] = 3; b[1] = 4;
> c[0] = 5; c[1] = 6;
>
>
> printf("Dummy print .... %i\n",c[0]);
> printf("argv[1] = %s\n",argv[1]);
> i = atoi(argv[1]);
> printf("index i = %i\n",i);
> printf("%i %i\n",b[i],a[i]);
> return 0;
> }
I recently found a real bug like this with valgrind
( see http://bugs.winehq.org/show_bug.cgi?id=25826,
in which the bug was caught because it ended up
causing an overlapping memcpy ). So it's worth
trying valgrind. And, luckily, Valgrind accidentally finds
something to complain about here (though it doesn't
point straight to the line in main() that causes it):
$ gcc -Wall -g -O2 -o code code.c
$ valgrind ./code -1
Dummy print .... 5
argv[1] = -1
index i = -1
Use of uninitialised value of size 4
at 0x4083B2B: _itoa_word (_itoa.c:195)
by 0x4087E55: vfprintf (vfprintf.c:1619)
by 0x412A054: __printf_chk (printf_chk.c:37)
2 68928292
- Dan
|