捕获奇怪的C指针算术错误

Eth*_*man 7 c pointers findbugs

我最近遇到了一个非常偷偷摸摸的错误,我忘了取消引用一个字符串(char数组)的指针,因此有时会覆盖堆栈上的一个字节.

坏:

char ** str;
(*str) = malloc(10);
...
str[2] = 'a'; //overwrites 3 bytes from the location in which str is stored
Run Code Online (Sandbox Code Playgroud)

更正:

char ** str;
(*str) = malloc(10);
...
(*str)[2] = 'a'; 
Run Code Online (Sandbox Code Playgroud)

GCC没有发出警告,这个错误会导致一个非常严重和真实的漏洞,因为它有时覆盖的值保持缓冲区的大小.我只抓住了这个bug,因为我很幸运,它导致了明显的失败.

  • 除了依靠运气和/或从不使用C进行任何操作外,您使用什么防御性编码技巧和技巧来捕获奇怪的C错误?

  • 我正在考虑搬到valgrind的MemCheck,有人用它吗?我怀疑它不会抓住这个bug.谁知道?

  • 是否有捕获指针解除引用或算术错误的工具?这甚至可能吗?

UPDATE

这是请求的示例代码,它不会抛出任何警告.

#include <stdlib.h>

void test(unsigned char** byteArray){
    (*byteArray) = (unsigned char*)malloc(5);
    byteArray[4] = 0x0;
}

int main(void){
    unsigned char* str;
    test(&str);  
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

编译不会导致错误:

gcc -Wall testBug.c -o testBug
Run Code Online (Sandbox Code Playgroud)

运行导致seg故障:

./testBug
Segmentation fault
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的GCC版本:

gcc -v

Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.1-4ubuntu9' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=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) 
Run Code Online (Sandbox Code Playgroud)

Chu*_*uck 3

我的最佳防御指针策略:强烈避免使用多于一层的间接。取消引用指针到指针来为其分配内存是可以的。但是,将分配的内存用作数组会带来麻烦,您也遇到了麻烦。我会把它做成这样的:

char **outStr;
*outStr = malloc(10);
char *str = *outStr;
str[2] = 10;
Run Code Online (Sandbox Code Playgroud)

好吧,实际上这只是一种保持理智的策略,同时也具有防御价值。当一次只有一层以上的间接寻址时,指针相当容易理解,并且当您充分理解它时,更容易使代码正常工作。