On 01/11/11 22:33, Peter Toft wrote:

>   Try to find the errors in this C/C++ snippet using valgrind:
>
>   #include<stdio.h>
>   /* Save as code.c */
>   int main(void)
>   {
>    int i=-1,a[2],b[2],c[2];
>    a[0] = 1; a[1] = 2;
>    b[0] = 3; b[1] = 4;
>    c[0] = 5; c[1] = 6;
>    printf("%i %in",b[i],a[i]);
>    return 0;
>   }
>
>   Compile using "gcc -o bla code.c -Wall" and check the code using
>   "valgrind ./bla".
>   Valgrind finds nothing even though I index a[-1] and b[-1] - not
>   good...

That's because you're using the wrong tool.

You're using the default memcheck tool, which will tell you about any 
use of uninitialised data, but because you are dealing with stack 
variables you have no gaps between your variables (with heap memory 
valgrind would ensure there was a gap) so an out of bounds access is 
likely to just access an adjacent (and defined) variable.

This should all be explained in the manual, which you probably want to 
read to understand what memcheck will and won't find.

The tool that might find this problem is exp-ptrcheck (in the released 
version) or exp-sgcheck (in the svn code). That specifically looks for 
out of bounds access to stack variables by using the debug information 
to discover the bounds.

Tom

-- 
Tom Hughes ([email protected])
http://compton.nu/

------------------------------------------------------------------------------
RSA&#174; Conference 2012
Save $700 by Nov 18
Register now&#33;
http://p.sf.net/sfu/rsa-sfdev2dev1
_______________________________________________
Valgrind-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to