Catching weird C pointer arithmetic bugs -


I recently went to a very stealth bug in which I forgot a director for a string (four array) And thus sometimes a byte on the stack is overwritten.

Bad:

  four ** str; (* Str) = Molk (10); ... str [2] = 'a'; // Overwrites 3 bytes in that location where str is stored  

Correct:

  char ** str; (* Str) = Molk (10); ... (* str) [2] = 'A'; The GCC did not give any warning, and this error would arise as a very serious and real exploitation, because this value is sometimes a value holding buffer size. I only caught this bug because I got good fortune and it causes a failure.  
  • Depend on destiny and / or never use it for C, which uses defensive coding techniques and tricks you use to catch horny insects. ?

  • I'm thinking about going to Valgrind, did anyone use it? I suspect that this bug will not be caught. Anyone know?

  • Is there pointer detection or tools to capture arithmetic bugs? Is this also possible?

    UPDATE

    Here is the requested example code, it does not make any warnings.

      # include & lt; Stdlib.h & gt; Zero test (unsigned char ** byte) {(bytere) = (unsigned char *) molk (5); Bytere [4] = 0x0; } Int main (zero) {unsigned char * str; Test (& str); Return 0; }  

    There is no error due to the compilation:

      GCC-Wall testBug.c -o testBug  

    The reason for running is Seg fault:

      ./ testBug partition fault  

    This is using GCC I version:

    < Pre> Use of GCC-V is built in the eyeglasses: i486-linux-gnu is configured with: ../src/configure -v --with-pkgversion = 'Ubuntu 4.4.1-4ubuntu9' - -with-bugurl = file: /// USR / share / doctor / gcc-4.4 / readme.bugs-cable-languages ​​= c, c ++, foreign, obese C, OBJ-C ++ - prefix = / usr - sign-sharing-compatible-multiprofessional - enable-linker-build-id - with-system-zlib --libexecdir = / usr / lib - without-included-gatetext - enabled threads = posix --with-gxx- include- dir = / usr / include / c ++ / 4.4 - program-suffix = -4.4 - enabled-NLS -enabled-clocel = gnu -enable- Libstdcxx-debug --enable-objc-gc --enable-targets = all --disable-werror -with-arch-32 = i486 --with-tune = generic --enable-checking = release --build = i486- Linux-gnu --host = i486-linux-gnu --target = i486-linux-gnu thread model: posix gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu9) "post-text" itemprop = " Text ">

    My best interests Shatmk indicator strategy: Avoid use indirection on more than one level. It is okay to fix this to assign a memory to point-to-pointers. But then using the memory assigned as an array is asking trouble, which you got. I would like to do something like this:

      char ** outStr; * OutStr = malloc (10); Char * str = * outStr; Str [2] = 10;  

    Okay, in fact this is only a removable-qualified strategy that is with defensive value. Indicators are quite easy to understand for a long time that there is no sign of more than one level at any one time, and when you understand it well, code is easy to work on.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -