Learn to read and understand sanitizer error messages 学习阅读和理解 sanitizer 错误信息
Sanitizers are tools that insert extra code into your program to detect various errors at runtime. In COMP2521, we use them to catch memory errors that would otherwise cause undefined behavior. Sanitizers 是在程序运行时检测各种错误的工具。在 COMP2521 中,我们使用它们来捕获会导致未定义行为的内存错误。
| Error Type错误类型 | Meaning含义 | First Reaction第一反应 |
|---|---|---|
heap-buffer-overflow |
Accessing memory outside allocated region访问超出分配区域的内存 | Array index out of bounds / malloc too small数组越界 / malloc 分配太小 |
heap-use-after-free |
Using memory after it's been freed使用已释放的内存 | Accessed pointer after free()free() 后又访问指针 |
use-of-uninitialized-value |
Reading uninitialized variable/field读取未初始化的变量/字段 | Forgot to initialize variable or struct field忘记初始化变量或结构体字段 |
SEGV / null pointer |
Accessing memory at address 0 (NULL)访问地址 0(NULL)的内存 | Dereferencing NULL pointer解引用空指针 |
memory-leak |
Allocated memory never freed分配的内存从未释放 | Missing free() for malloc()malloc() 后缺少 free() |
Your program tried to read or write outside the allocated memory region. 你的程序尝试在分配的内存区域之外读取或写入。
arr[5] when size is 5)数组索引越界(如大小为 5 时访问 arr[5])sizeof(pointer) instead of sizeof(struct))sizeof() 使用错误(如 sizeof(pointer) 而不是 sizeof(struct))
Your program accessed memory after it was freed with free().
你的程序在用 free() 释放内存后又访问了它。
Imagine you returned a locker to the gym. You no longer own it. If you try to open it again, you might find someone else's stuff inside, or the locker might be demolished. The same applies to freed memory. 想象你把储物柜还给了健身房。你不再拥有它。如果你再试图打开它,可能会发现里面是别人的东西, 或者储物柜可能已经被拆除。释放的内存也是一样的道理。
Save any data you need BEFORE calling free()!
在调用 free() 之前保存你需要的数据!
Your program read a variable or struct field that was never assigned a value. 你的程序读取了一个从未被赋值的变量或结构体字段。
Your program tried to access memory at address 0 (NULL). This is always invalid. 你的程序尝试访问地址 0(NULL)的内存。这永远是无效的。
while (curr != NULL) exits when curr becomes NULL.
while (curr->next != NULL) exits when curr is the last node.
while (curr != NULL) 在 curr 变成 NULL 时退出。
while (curr->next != NULL) 在 curr 是最后一个节点时退出。
Look for the text after "ERROR: AddressSanitizer:"查找 "ERROR: AddressSanitizer:" 后面的文字
Look for lines with .c files - that's your code!查找带有 .c 文件的行 - 那是你的代码!
This tells you where the memory was created.这告诉你内存是在哪里创建的。
This tells you how much memory was allocated.这告诉你分配了多少内存。
| Error错误 | Likely Fix可能的修复 |
|---|---|
heap-buffer-overflow |
Check array bounds, verify malloc size检查数组边界,验证 malloc 大小 |
heap-use-after-free |
Save data BEFORE free()在 free() 之前保存数据 |
use-of-uninitialized-value |
Initialize all struct fields / array elements初始化所有结构体字段 / 数组元素 |
SEGV / null pointer |
Check loop conditions, handle NULL cases检查循环条件,处理 NULL 情况 |
memory-leak |
Add free() for every malloc()为每个 malloc() 添加 free() |