我的工作内容。
测试1
1 #include <stdio.h>
2
3 int test[16];
4
5 int main()
6 {
7 test[17] = -1;
8 }
/tmp $ gcc ./main.c -o main -fsanitize=address
/tmp $ ./main
/tmp $
测试2
1 #include <stdio.h>
2
3 int test[16] = {1};
4
5 int main()
6 {
7 test[17] = -1;
8 }
/tmp $ gcc ./main.c -o main -fsanitize=address
/tmp $ ./main
=================================================================
==19776==ERROR: AddressSanitizer: global-buffer-overflow on address
...
看起来全局缓冲溢出检测对于存储在bss中的全局变量不起作用(是这样吗?)。这背后的原因是什么?
更新: 执行存储操作的代码并没有被优化掉。 系统信息:
$ gcc --version
gcc (Ubuntu 7.2.0-8ubuntu3.2) 7.2.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
clang
吗? - Jean-François Fabrevolatile
也没有可观察的行为),那么这应该是可以的,但如果这取决于变量是否初始化,我会感到惊讶... - user2371524