You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用GCC编译时AddressSanitizer未显示错误位置求助

问题:AddressSanitizer未显示代码行号的解决方法

编译存在heap-use-after-free问题的C代码时,使用命令gcc uaf.c -o uaf -fsanitize=address -static-libasan,运行后AddressSanitizer的错误摘要仅显示内存地址相关信息,无法定位到具体代码文件及行号。期望输出类似SUMMARY: AddressSanitizer: heap-use-after-free /home/test/work/uaf.c:8 in memcpy的内容,请问遗漏了什么配置?

测试代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main(int argc, const char *argv[]) {
    char *s = malloc(100);
    free(s);
    strcpy(s, "Hello world!");
    printf("string is: %s\n", s);
    return 0;
}

当前错误摘要

SUMMARY: AddressSanitizer: heap-use-after-free (/home/test/work/uaf+0x1db52) in memcpy

解决方法

  • 编译时必须添加调试信息生成参数-g。该参数会让编译器在可执行文件中嵌入代码行号、文件名等调试元数据,AddressSanitizer才能将内存地址映射到具体的代码位置。
  • 修改后的完整编译命令:
    gcc uaf.c -o uaf -g -fsanitize=address -static-libasan
    
  • 补充说明:-g默认生成的调试信息足以满足AddressSanitizer的需求,无需额外指定级别(如-g3);静态链接libasan不影响调试信息的生成,只需确保保留-g参数即可。

内容的提问来源于stack exchange,提问作者gopher

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 05:18:22