最近、非常に卑劣なバグに遭遇しました。このバグでは、文字列(char配列)へのポインタを逆参照するのを忘れて、スタックの1バイトを上書きすることがありました。
悪い:
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)