vector<int> v(5);
int x = v[5]; // 发生越界、最后一个元素是 v[4]
cout << x << endl;
这里访问 x 的时候越界了,但编译运行的时候没有任何提示:
g++ ./a.cpp && ./a.out
在运行时检测,越界时报错,通过堆栈信息可以找出越界的代码:
# g++ -fsanitize=address -fno-omit-frame-pointer -g ./a.cpp && ./a.out
=================================================================
==557143==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000054 at pc 0x000000401676 bp 0x7ffcec886380 sp 0x7ffcec886378
READ of size 4 at 0x603000000054 thread T0
#0 0x401675 in main a.cpp:10
#1 0x7f5629f0824d in __libc_start_call_main (/nix/store/c6f52mvbv0d8rd3rlslsvy7v4g3pmm7p-glibc-2.35-163/lib/libc.so.6+0x2924d)
#2 0x7f5629f08308 in __libc_start_main_impl (/nix/store/c6f52mvbv0d8rd3rlslsvy7v4g3pmm7p-glibc-2.35-163/lib/libc.so.6+0x29308)
#3 0x4017d4 in _start (/home/luo/code/cpp-exercise/a.out+0x4017d4)
SUMMARY: AddressSanitizer: heap-buffer-overflow a.cpp:10 in main