惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Cyberwarzone
Cyberwarzone
C
CERT Recently Published Vulnerability Notes
Hacker News: Ask HN
Hacker News: Ask HN
AI
AI
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
Project Zero
Project Zero
Security Latest
Security Latest
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
P
Proofpoint News Feed
K
Kaspersky official blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
P
Privacy International News Feed
P
Privacy & Cybersecurity Law Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
PCI Perspectives
PCI Perspectives
博客园 - Franky
C
Cisco Blogs
aimingoo的专栏
aimingoo的专栏
Stack Overflow Blog
Stack Overflow Blog
T
Tor Project blog
N
Netflix TechBlog - Medium
The Last Watchdog
The Last Watchdog
Know Your Adversary
Know Your Adversary
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
The GitHub Blog
The GitHub Blog
Latest news
Latest news
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - 叶小钗
H
Hacker News: Front Page
S
Secure Thoughts
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
阮一峰的网络日志
阮一峰的网络日志
S
Schneier on Security
Blog — PlanetScale
Blog — PlanetScale
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
腾讯CDC
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
量子位
L
LINUX DO - 热门话题

浮生笔记

时机的重要性 回答dayu博客的几个问题 学会做选择 我理想的城市 如何选择一个适合的城市 马来西亚游记 回首2023,展望2024 从ChatGPT聊起 菲律宾旅游攻略笔记 深圳游记 香港游记 怎样赚钱----我理解的商业模式 用户界面与跨平台 使用telnet通过IMAP协议读取QQ邮箱 信息成本-----从ChatGPT到情报学 买断制到订阅制----兼谈消费频次 我眼中的CSDN 迁移博客内容到静态博客 Hugo使用Jane主题支持搜索实现 C语言和Lua的相互调用示例代码 从放逐之城看经济学 不同产业的分析 回首2021,展望2022 游戏中的经济学 Windows获取网络地址、子网掩码等 C++ 编译器支持标准判断 C++ 编译器支持标准判断 VS Code的golang开发配置 之 代码提示 二叉树遍历的非递归算法的实现 我的Chrome插件 golang 获取get参数 关于写代码的几个看法 golang编程之我见 Linux 网络编程之 Select 构建之法读书笔记 (1) 友情链接 Effective Morden C++ 读书笔记(3) 二进制协议 vs 文本协议 从重构到重写 asio制作使用ssl通信的证书 gdb 7.11 Linux 获取网卡信息 《构建之法》读后感 由买冰箱想到的 2014年年终总结 聊聊我对写好程序的认识 编程技巧之表格驱动编程 经验搜索排名---google已经做过类似的了(我想多了) 有关编程语言的认识
-fsanitize=address 参数作用
DT · 2019-01-20 · via 浮生笔记

文章目录

在C++的开发中,我们经常会遇到很多和内存有关的错误。其实使用编译器的一些检查功能,可以帮助我们一定程度上减少这样的错误,下面我们介绍一下 -fsanitize=address 的作用以及一些用法。

1.参数介绍

介绍的部分参考:链接

2.主要检查的方面

  • Out-of-bounds accesses to heap, stack and globals
  • Use-after-free
  • Use-after-return (runtime flag ASAN_OPTIONS=detect_stack_use_after_return=1)
  • Use-after-scope (clang flag -fsanitize-address-use-after-scope)
  • Double-free, invalid free
  • Memory leaks (experimental)

3.代码示例

3.1 检查地址访问越界

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
//g++ -O0 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize=address ./Out_of_bounds.cpp
#include <iostream>
char g_ch[5]={0};
int main(int argc,char * argv[])
{
    std::cout << g_ch[5] << std::endl;
    char ch_local[6]={0};

    std::cout << ch_local[6] << std::endl;
    for(int i = 0 ; i   7 ; i++)
    {
        std::cout << ch_local[i] << std::endl;
    }

    char * pChar = new char[5];
    for(int i = 0 ; i < 7 ; i++)
    {
        std::cout << pChar[i] << std::endl;
    }
    delete [] pChar;
    return 0;
}

程序的输出:out_of_bounds.png

3.2 检查释放后再次使用变量

代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
//g++ -O0 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize=address ./Out_of_bounds.cpp

#include  iostream>
int main(int argc,char* argv[])
{
    char * pChar = new char;
    *pChar = '1';
    std::cout<<  *pChar  <<std::endl;
    delete pChar;
    *pChar = '2';
    std::cout << *pChar << std::endl;
    return 0;
}

程序输出:user_after_free.png

3.3 内存泄露

程序代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//g++ -O0 -fno-omit-frame-pointer -fno-optimize-sibling-calls -fsanitize=address ./Out_of_bounds.cpp

#include <iostream>
void MemLeakOne()
{
    char * pChar = new char;
}

void MemLeakTwo(int x)
{
    char * pChar = new char;
    if(x == 0)
    {
        delete pChar;
    }
}

void MemLeakThree()
{
    char * pChar = new char[12];
    delete []pChar;
}
int main(int argc,char * argv[])
{
    MemLeakOne();
    MemLeakTwo(2);
    MemLeakThree();
    return 0;
}

编译警告:mem_leak_warn.png 程序输出:mem_leak2.png

文章作者 DT

上次更新 2019-01-28

许可协议 CC BY-NC-ND 4.0