1.按下开关Clang

sudo apt-get install Clang

2.编写测试程序  memleak.c

#include<stdio.h>
#include<stdlib.h> int main()
{
int *mem;
mem=malloc(sizeof(int));
if(mem) return 1;
*mem=0xdeadbeaf;
free(mem);
return 0;
}

3. 执行代码静态检查

scan-build -o memleak gcc memleak.c -o m

结果例如以下:

memleak.c:9:5: warning: Dereference of null pointer (loaded from variable 'mem')

    *mem=0xdeadbeaf;

    ^~~~

1 warning generated.

scan-build: 1 bugs found.

4.在当前目录下会生存一个 memleak目录,内有一个以日期命名的目录,用浏览器打开该目录内的 index.html 查看错误bug报告

5.改动 memleak.c

#include<stdio.h>
#include<stdlib.h> int main()
{
int *mem;
mem=malloc(sizeof(int));
if(mem==NULL) return 1;
*mem=0xdeadbeaf;
free(mem);
return 0;
}

6.再次执行 scan-build -o memleak gcc memleak.c -o m

7. 此时bug消失

8. 下载开源 cgdb http://cgdb.github.io/

9.切换到 cgdb 所在文件夹,执行 scan-build -o cgdb-scan ./configure --prefix=$PWD/build

10. 执行 scan-build -o cgdb-scan make

11.用浏览器打开当前文件夹下 cgdb-scan 下的 index.html 查看 bug 报告

參考文献

http://events.linuxfoundation.org/images/stories/pdf/lcjp2012_matsumotoh.pdf

http://clang-analyzer.llvm.org/scan-build.html

http://www.hackhowtofaq.com/blog/static-analysis-with-llvm-clang-in-linux/

版权声明:本文博客原创文章,博客,未经同意,不得转载。

[Linux]使用Clang实现代码静态分析的更多相关文章

  1. Understand:高效代码静态分析神器详解(转)

    之前用Windows系统,一直用source insight查看代码非常方便,但是年前换到mac下面,虽说很多东西都方便了,但是却没有了静态代码分析工具,很幸运,前段时间找到一款比source ins ...

  2. Understand:高效代码静态分析神器详解(一)

    Understand:高效代码静态分析神器详解(一) Understand   之前用Windows系统,一直用source insight查看代码非常方便,但是年前换到mac下面,虽说很多东西都方便 ...

  3. Understand:高效代码静态分析神器详解(一) | 墨香博客 http://www.codemx.cn/2016/04/30/Understand01/

    Understand:高效代码静态分析神器详解(一) | 墨香博客 http://www.codemx.cn/2016/04/30/Understand01/ ===== 之前用Windows系统,一 ...

  4. Understand:高效代码静态分析神器详解(一)【转】

    转自:http://www.codemx.cn/2016/04/30/Understand01/ 之前用Windows系统,一直用source insight查看代码非常方便,但是年前换到mac下面, ...

  5. 代码静态分析工具-splint的学习与使用[转]

    代码静态分析工具--splint的学习与使用[转] 引言 最近在项目中使用了静态程序分析工具PC-Lint,体会到它在项目实施中带给开发人员的方便.PC-Lint是一款针对C/C++语言.window ...

  6. C/C++代码静态分析工具调研

    C/C++代码静态分析工具调研 摘自:https://www.jianshu.com/p/92886d979401 简述 静态分析(static analysis)是指在不执行代码的情况下对其进行分析 ...

  7. Understand:高效代码静态分析神器详解(一) 转

    之前用Windows系统,一直用source insight查看代码非常方便,但是年前换到mac下面,虽说很多东西都方便了,但是却没有了静态代码分析工具,很幸运,前段时间找到一款比source ins ...

  8. infer 代码静态分析

    infer 代码静态分析 静态代码分析工具,主要是为了提高我们的代码质量. 通常,我们提高代码质量的方式是通过CodeReview,但是这个过程耗费的人工和时间往往较大.并且随着代码量的增加人肉检测起 ...

  9. C/C++代码静态分析插件 SourceInsight_Scan

    sourceinsight-scan 是一款集成在 SourceInsight 中的c/c++代码静态分析插件,集成了cppcheck,coverity,pclint等业界优秀的静态分析工具的优点. ...

随机推荐

  1. A WPF File ListView and ComboBox

    源码下载: Download FileListView_Version_2.zip Download FileListView_Version_2_Binaries.zip Download File ...

  2. 北邮iptv用WindowsMediaplayer打不开的解决的方法

    前言:之前我的iptv能够用,可是有次我安装了realplayer,它就偷偷把iptv文件的默认打开方式给篡改了,卸载了                  realplayer之后,iptv不能直接用 ...

  3. UVA 11388-GCD LCM(数学)

    I I U C   O N L I N E   C  Problem D: GCD LCM Input: standard input Output: standard output The GCD ...

  4. [LeetCode107]Binary Tree Level Order Traversal II 二叉树层次遍历

    题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...

  5. Linux 环境下 Lua 安装(转)

    系统环境:CentOS-6.2-x86_64. Lua 是嵌入式脚本语言,应用场景很广泛. 引自官网:Lua is used in many products and projects around ...

  6. EL字符串表达式和常用功能用途拦截

    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> ${wj ...

  7. 源代码版本控制工具TortoiseSVN,AnkhSVN最新版本下载地址

    TortoiseSVN http://tortoisesvn.net/downloads.html 页面下部有中文语言补丁 AnkhSVN https://ankhsvn.open.collab.ne ...

  8. NVIDIA+关联2015写学校招收评论(嵌入式方向,上海)

    我没有写很长一段时间Blog中的一个,在过去的几个月中还没有看到太多的生长技术,来来回回一直在做的事情,要毕业找工作,但发现并没有冷静下来,准备过.这不是让人觉得暂时补习班是凡人啊. 本科不试试.那你 ...

  9. webBrower控件实现winform和webpage交互

    添加WebBrowser控件 private WebBrowser webBrowser1; 引用页面的document对象 HtmlDocument doc = webBrowser1.Docume ...

  10. Java设计模式菜鸟系列(两)建模与观察者模式的实现

    转载请注明出处:http://blog.csdn.net/lhy_ycu/article/details/39755577 观察者(Observer)模式定义:在对象之间定义了一对多的依赖关系,这样一 ...