C++内存泄漏、数组越界……检测工具:Sanitizers

g++ gcc自带的sanitizers可用于检测内存泄漏、数组越界、线程竞态条件、未初始化等问题,支持Linux(win暂时只支持Aaddress sanitizers)。

用法:https://blog.csdn.net/weixin_41644391/article/details/103450401

CMAKE中的用法:

Google sanitizers

Sanitizers are open-source tools for dynamic code analysis designed by Google. CLion integrates with the following Sanitizers:

  • AddressSanitizer (ASan)
  • LeakSanitizer (LSan)
  • ThreadSanitizer (TSan)
  • UndefinedBehaviorSanitizer (UBSsan)
  • MemorySanitizer (MSan)

Useful links: Official documentation, Sanitizers in Clang, Sanitizers among Program Instrumentation Options in GCC.

Sanitizers are implemented in Clang starting 3.1 and GCC starting 4.8. All the sanitizers are available on Linux x86_64 machines and also on Windows 10 with clang-cl. For macOS, only AddressSanitizer and UndefinedBehaviorSanitizer are supported.

As Sanitizers are based on compiler instrumentation, you need to rebuild your project in order to start using these tools.

Configure Sanitizers

Specify compiler flags

Adjust the following template line and add it to your CMakeLists.txt:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=[sanitizer_name] [additional_options] [-g] [-OX]")

note
Use CMAKE_C_FLAGS instead of CMAKE_CXX_FLAGS for C projects.

For [sanitizer_name] use one of the following:

  • address for AddressSanitizer
  • leak for LeakSanitizer
  • thread for ThreadSanitizer
  • undefined for UndefinedBehaviorSanitizer (other options are also available, see the UBSan section)
  • memory for MemorySanitizer

[Additional_flags] are other compilation flags, such as -fno-omit-frame-pointer, fsanitize-recover/fno-sanitize-recover, -fsanitize-blacklist, etc.

Use [-g] to have file names and line numbers included in warning messages.

Add optimization level [-OX] to get reasonable performance (see recommendations in the particular Sanitizer documentation).

详细:https://www.jetbrains.com/help/clion/google-sanitizers.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值