【发布时间】:2010-02-02 18:10:55
【问题描述】:
我最近遇到了一个非常狡猾的错误,我忘记取消引用指向字符串(char 数组)的指针,因此有时会覆盖堆栈上的一个字节。
不好:
char ** str;
(*str) = malloc(10);
...
str[2] = 'a'; //overwrites 3 bytes from the location in which str is stored
更正:
char ** str;
(*str) = malloc(10);
...
(*str)[2] = 'a';
GCC 没有产生任何警告,这个错误会导致一个非常严重和真实的漏洞,因为它有时覆盖的值包含缓冲区的大小。我只是因为幸运而发现了这个错误,并且它导致了明显的失败。
除了依靠运气和/或从不使用 C 做任何事情之外,您还使用哪些防御性编码技术和技巧来捕获奇怪的 C 错误?
我正在考虑转到 valgrind 的 MemCheck,有人用过吗?我怀疑它不会发现这个错误。有人知道吗?
是否有用于捕获指针取消引用或算术错误的工具?这可能吗?
更新
这是请求的示例代码,它不会引发任何警告。
#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;
}
编译不会出错:
gcc -Wall testBug.c -o testBug
运行导致段错误:
./testBug
Segmentation fault
这是我正在使用的 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)
【问题讨论】:
-
我从这样做得到一个警告:“警告:赋值使指针从整数不进行强制转换”。
-
@Jefromi 试试我刚刚发布的内容,我当然没有收到警告。我也在用 -Wall 编译。
-
您的示例代码分配了
0x0,而不是初始问题描述中的'a'。编译器不会警告分配 0,因为它是一个空指针常量。编辑:哦,R Samuel Klatchko 已经在另一个线程中说过...... -
我的评论提到了您的原始代码,这肯定会产生警告。