开源代码检查工具

Cppcheck
cppcheck是静态的C/C++ 代码分析工具,用以检查内存泄漏,错配的内存分配和释放,缓冲区溢出等问题。支持eclipse插件。
Someof the checks that are supported include:

    http://en.wikipedia.org/wiki/Cppcheck


    Splint:

    Splint是静态检查C语言安全弱点和编写错误的程序。检查主要包括:未使用的变量,类型不一致,使用未定义的变量,内存管理错误(内存泄露,悬浮指针等),缓冲区溢出,无法执行到的程序,忽略返回值,无限循环等。

    Problemsdetected by Splint include:

    •  Dereferencing a possibly null pointer (Section 2);
    •  Using possibly undefined storage or returning storage that is not properly defined (Section 3);
    •  Type mismatches, with greater precision and flexibility than provided by C compilers (Section 4.1–4.2);
    •  Violations of information hiding (Section 4.3);
    •  Memory management errors including uses of dangling references and memory leaks  (Section 5);
    •  Dangerous aliasing (Section 6);
    •  Modifications and global variable uses that are inconsistent with specified interfaces (Section 7);
    •  Problematic control flow such as likely infinite loops (Section 8.3.1), fall through cases or incomplete switches (Section 8.3.2), and suspicious statements (Section 8.4);
    •  Buffer overflow vulnerabilities (Section 9);
    •  Dangerous macro implementations or invocations (Section 11); and
    •  Violations of customized naming conventions.  (Section 12)
      .
      源文档 <http://splint.org/manual/html/sec1.html>

    经过初步测试splint相对于cppcheck更为强大,而且当前版本更为稳定。


    Valgrind:

    Valgrind是一个用于检查程序内存泄漏、段错误等bug的工具。它包含的有:内存检测、缓存检测、代码覆盖、性能测试等功能。Valgrind的最初作者是Julian Seward,他于2006年由于在开发Valgrind上的工作获得了第二届Google-O'Reilly开源代码奖

    Valgrind是运行在Linux上一套基于仿真技术的程序调试和分析工具,它包含一个内核——一个软件合成的CPU,和一系列的小工具,每个工具都可以完成一项任务──调试,分析,或测试等。Valgrind可以检测内存泄漏和内存违例,还可以分析cache的使用等,灵活轻巧而又强大,能直穿程序错误的心脏,真可谓是程序员的瑞士军刀。它包括Memcheck,Callgrind, Cachegrind, helgrind等一些列的工具。

    Memcheck是其中最常用的工具,用来检测程序中出现的内存问题,所有对内存的读写都会被检测到,一切对malloc()/free()/new/delete的调用都会被捕获。所以,它能检测以下问题:

    1.对未初始化内存的使用;

    2.读/写释放后的内存块;

    3.读/写超出malloc分配的内存块;

    4.读/写不适当的栈中内存块;

    5.内存泄漏,指向一块内存的指针永远丢失;

    6.不正确的malloc/free或new/delete匹配;

    7,memcpy()相关函数中的dst和src指针重叠。


    一些常用的选项:

           查考其帮助 valgrind -h       or   info valgrind


    我们通过例子看一下它的具体使用。我们构造一个存在内存泄漏的C程序,如下:

    /* create by guolb57 valgrind_test.c */
    /* gcc -Wall -g -o valgrind_test -o valgrind_test.c*/
    /* use valgrind: 
           valgrind --tool=memcheck ./valgrind_test*/
    /*or   valgrind --leak-check=full ./valgrind_test*/
    #include <stdio.h>
    #include <stdlib.h>
    
    int valgrind_test()
    {
        int *p = malloc(10 * sizeof(int));
    
        p[10] = 0;   /* problem 1: heap block overrun */
    
        return 0;
    }  /* problem 2: memory leak, p not be freed */
    
    int main(int argc, const char *argv[])
    {
        valgrind_test();
        printf("hello world\n");
        return 0;
    }

    我们得到如下错误信息:



    我们可以很清楚地看出,分配和释放了多少内存,有多少内存泄漏。

    这对我们查找内存泄漏十分方便。然后我们重新编译程序并绑定调试器:


    Artistic Style Eclipse Plugin 是一个用来对C/C++代码进行格式化的 Eclipse 插件,可在 Eclipse CDT 环境中使用.可以用来定义基本的编码格式风格,比如代码缩进,* +等运算符两端加上空格的,不同模块间添加空行

    CppNcss复杂度检查工具,可度量函数级,文件级,工程级的复杂度


    总结:cppchecksplint是静态的代码检查工具,valgrind为动态的代码检查工具。主要检查对象都是内存管理错误。valgrind由于前两者,它得到更为丰富准确的信息。但是由于它提供了大量的信息,对于是否是bug的鉴定更为困难。ArtisticStyle Eclipse Plugin能在基本的代码格式风格和规范上起到一定的帮助作用。

     

    • 2
      点赞
    • 12
      收藏
      觉得还不错? 一键收藏
    • 1
      评论

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论 1
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值