简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长!
优质专栏:Audio工程师进阶系列【原创干货持续更新中……】
人生格言: 人生从来没有捷径,只有行动才是治疗恐惧和懒惰的唯一良药.
本篇目的:理解C++之调试内存访问错误手段。
1 #include <cstdio>
2
3 int main(void) {
4 int a[5] = {0};
5 //访问数组索引5,越界.
6 a[5] = 2;
7
8 printf("%d\n", a[5]);
9 return 0;
10 }
编译:g++ -fsanitize=address test.cpp -g -o test
运行: ./test
=================================================================
==269033==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffeaa98aae4 at pc 0x55978dd8839a bp 0x7ffeaa98aaa0 sp 0x7ffeaa98aa90
WRITE of size 4 at 0x7ffeaa98aae4 thread T0
#0 0x55978dd88399 in main ~/test.cpp:6
#1 0x7f8010ea5d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
#2 0x7f8010ea5e3f in __libc_start_main_impl ../csu/libc-start.c:392
#3 0x55978dd88184 in _start (~/test+0x1184)
Address 0x7ffeaa98aae4 is located in stack of thread T0 at offset 52 in frame
#0 0x55978dd88258 in main ~/test.cpp:3
This frame has 1 object(s):
[32, 52) 'a' (line 4) <== Memory access at offset 52 overflows this variable
HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork
(longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow ~/test.cpp:6 in main
Shadow bytes around the buggy address:
0x100055529500: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100055529510: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100055529520: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100055529530: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100055529540: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x100055529550: 00 00 00 00 00 00 f1 f1 f1 f1 00 00[04]f3 f3 f3
0x100055529560: f3 f3 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100055529570: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100055529580: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100055529590: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x1000555295a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
Shadow gap: cc
==269033==ABORTING
SUMMARY: AddressSanitizer: stack-buffer-overflow ~/test.cpp:6 in main
第6行内存溢出, 访问数组索引5越界,导致内存溢出。