Sanvs gcc与AddressSanitizer的行为差异 [英] clang vs gcc behavioral difference with AddressSanitizer

查看:196
本文介绍了Sanvs gcc与AddressSanitizer的行为差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个内存泄漏的示例代码.尽管clang正确显示了泄漏,但是我无法使用gcc实现相同的功能.我正在使用的gcc版本是4.8.5-39

I have a sample code with memory leaks. Though clang displays the leak correctly, I am unable to achieve the same with gcc. gcc version I am using is 4.8.5-39

代码:

#include <stdlib.h>
void *p;
int main() {
  p = malloc(7);
  p = 0; // The memory is leaked here.
  return 0;
}

CLANG:

clang -fsanitize=address -g memory-leak.c ; ASAN_OPTIONS=detect_leaks=1 ./a.out

=================================================================
==15543==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 7 byte(s) in 1 object(s) allocated from:
    #0 0x465289 in __interceptor_malloc (/u/optest/a.out+0x465289)
    #1 0x47b549 in main /u/optest/memory-leak.c:4
    #2 0x7f773fe14544 in __libc_start_main /usr/src/debug/glibc-2.17-c758a686/csu/../csu/libc-start.c:266

SUMMARY: AddressSanitizer: 7 byte(s) leaked in 1 allocation(s).

海湾合作委员会:

gcc -fsanitize=address -g memory-leak.c ; ASAN_OPTIONS=detect_leaks=1 ./a.out

我需要使用gcc.有人可以帮我了解为什么gcc的行为不像clang吗,我应该怎么做才能使其工作.

I need to use gcc. Can someone help me understand why gcc isn't behaving like clang and what should I do to make it work.

推荐答案

有人可以帮我理解为什么gcc的行为不像c声

Can someone help me understand why gcc isn't behaving like clang

Gcc-4.8 已于2013年3月22日发布.

2013年11月15日发送了支持-fsanitize=leak >的补丁,并且可能不会向后移植到4.8版本中.

The patch to support -fsanitize=leak was sent on Nov. 15, 2013, and is likely not backported into the 4.8 release.

GCC-8.3可以轻松检测到此泄漏:

GCC-8.3 has no trouble detecting this leak:

$ gcc -g -fsanitize=address memory-leak.c && ./a.out

=================================================================
==166614==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 7 byte(s) in 1 object(s) allocated from:
    #0 0x7fcaf3dc5330 in __interceptor_malloc (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xe9330)
    #1 0x55d297afc162 in main /tmp/memory-leak.c:4
    #2 0x7fcaf394052a in __libc_start_main ../csu/libc-start.c:308

SUMMARY: AddressSanitizer: 7 byte(s) leaked in 1 allocation(s).

这篇关于Sanvs gcc与AddressSanitizer的行为差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆