zoukankan      html  css  js  c++  java
  • [Linux]使用Clang实现代码静态分析

    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/






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

  • 相关阅读:
    Session共享的解决方案
    用IIS配置反向代理
    authorization配置
    git之https或http方式设置记住用户名和密码的方法
    微信分享接口
    为你的Visual Studio单独设置代理服务器
    HTTP错误404.13
    MVC5的AuthorizeAttribute详解
    【MVC5】画面多按钮提交
    PetaPoco dynamic
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4747792.html
Copyright © 2011-2022 走看看