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. (hdu step 7.1.2)You can Solve a Geometry Problem too(乞讨n条线段,相交两者之间的段数)

    称号: You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/ ...

  2. TotoiseSVN基本用法

    TotoiseSVN的基本用法 TotoiseSVN的基本用法 一.签入源码到SVNserver 假如我们使用Visual Studio在目录StartKit中创建了一个项目.我们要把这个项目的源码签 ...

  3. 邮箱password复位图

    blog宗旨:用图说话 辅助文字说明: 长处:用户系统完毕接口部分.界面留给业务自行设计. 缺点:邮箱发送邮件定制模板,没有完毕松耦合. 改进点:邮箱的准备发送邮件模板. 版权声明:本文博客原创文章, ...

  4. Code::Blocks项目配置基础

    File 菜单 New :新建( Empty file/file . class . project . build target ) . Recent projects/files :近期打开的项目 ...

  5. 360的IM可能会是什么样?

    尽管周鸿祎不止一次的说过,它不会进入IM领域,可是在360和QQ大战之后,很多用户 在卸载了QQ,寻找替代的IM工具的时候,发现他们非常难找到合适的替代IM工具,由于 好友非常难迁移.在用户的强烈 呼 ...

  6. [LeetCode238]Product of Array Except Self

    题目: Given an array of n integers where n > 1, nums, return an array output such that output[i] is ...

  7. lua、groovy嵌入到java中的性能对比(转)

    lua和groovy都是可以嵌入到java中的脚本语言.lua以高性能著称,与C/C++在游戏开放中有较多使用,groovy是一个基于Java虚拟机(JVM)的敏捷动态语言,在jvm下有着不错的性能. ...

  8. hdu 1150 Machine Schedule(最小顶点覆盖)

    pid=1150">Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/327 ...

  9. JS于string 和 json互转对象

    一.json开启string JSON.stringify(jsonObj) 两.string开启json eval(string) 版权声明:本文博主原创文章.博客,未经同意不得转载.

  10. Xcode_6.3_beta_4 官方 下载地址

    http://adcdownload.apple.com//Developer_Tools/Xcode_6.3_beta_4/Xcode_6.3_beta_4.dmg