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. js匀速运动停止条件

    匀速运动,怎么让它到达指定位置时停止呢? 原理: 1,物体和目标的差值距离小于等于速度时,即停止 2,接着让物体移动位置等于目标位置 示例:匀速运动停止 html部分 <input type=& ...

  2. Unbound classpath container: &#39;JRE System Library [jdk17060]&#39; in project ***

    项目报告的错误列表 Unbound classpath container: 'JRE System Library [jdk17060]' in project **** 误. 原因是,我升级JDK ...

  3. Oracle 11G DataGuard生产环境又一次启动具体过程

     场景,重新启动数据库,不重新启动linux系统,所以不用考虑监听程序,#linux输入lsnrctl start1 数据库关闭1.1 关闭主库SHUTDOWN IMMEDIATE; SQL> ...

  4. 高速压缩跟踪(fast compressive tracking)(CT)算法分析

    本文为原创,转载请注明出处:http://blog.csdn.net/autocyz/article/details/44490009 Fast Compressive Tracking (高速压缩跟 ...

  5. Redis是新兴的通用存储系统-为何Redis要比Memcached好用

    GitHub版本地址: https://github.com/cncounter/translation/blob/master/tiemao_2014/Redis_beats_Memcached/R ...

  6. 解决java.sql.SQLException: ORA-01789: query block has incorrect number of result columns

    java.sql.SQLException: ORA-01789: query block has incorrect number of result columns at oracle.jdbc. ...

  7. DM8168 新三板系统启动

    DM8168从补丁到系统的新董事会开始折腾了20天,最终完成,高校是累的东西,导师只焊接机10一个BGA,其他人则手. 前段时间启动操作系统时,到了Starting Matrix GUI applic ...

  8. LVM pvcreate,vgcreate,lvcreate,mkfs

    首先介绍LVM的几个概念:     1. 物理卷Physical volume (PV):可以在上面建立卷组的媒介,可以是硬盘分区,也可以是硬盘本身或者回环文件(loopback file).物理卷包 ...

  9. MAC OSX 进程间通信

    Mac OS在下面IPC方式很多类型,大约如下. 1. Mach API  2. CFMessagePort  3. Distributed Objects (DO) NSDistributedNot ...

  10. tomcat配置sqlserver数据库

    1. 首先确保Tomcat安装文件夹中的\common\lib(对于Tomcat5.5)或者是\lib(Tomcat6.0)文件夹中已包括JDBC连接数据库所必须的三个.jar文件(msbase.ja ...