開源C++/C代碼檢查工具

Cppcheck
cppcheck是靜態的C/C++ 代碼分析工具,用以檢查內存泄漏,錯配的內存分配和釋放,緩衝區溢出等問題。支持eclipse插件。
Someof the checks that are supported include:css

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


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)
    .源文檔 <<A href="http://splint.org/manual/html/sec1.html">http://splint.org/manual/html/sec1.html>

通過初步測試splint相對於cppcheck更爲強大,並且當前版本更爲穩定。eclipse

 

Valgrind:ide

Valgrind是一個用於檢查程序內存泄漏、段錯誤等bug的工具。它包含的有:內存檢測、緩存檢測、代碼覆蓋、性能測試等功能。Valgrind的最初做者是Julian Seward,他於2006年因爲在開發Valgrind上的工做得到了第二屆Google-O'Reilly開源代碼獎。函數

Valgrind是運行在 Linux上一套基於仿真技術的程序調試和分析工具,它包含一個內核——一個軟件合成的CPU,和一系列的小工具,每一個工具均可以完成一項任務──調試,分析,或測試等。Valgrind能夠檢測內存泄漏和內存違例,還能夠分析cache的使用等,靈活輕巧而又強大,能直穿程序錯誤的心臟,真可謂是程序員的瑞士軍刀。它包括Memcheck,Callgrind, Cachegrind, helgrind等一些列的工具。工具

Memcheck是其中最經常使用的工具,用來檢測程序中出現的內存問題,全部對內存的讀寫都會被檢測到,一切對malloc()/free()/new/delete的調用都會被捕獲。因此,它能檢測如下問題:oop

1.對未初始化內存的使用;

2.讀/寫釋放後的內存塊;

3.讀/寫超出malloc分配的內存塊;

4.讀/寫不適當的棧中內存塊;

5.內存泄漏,指向一塊內存的指針永遠丟失;

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

7,memcpy()相關函數中的dst和src指針重疊。

 

一些經常使用的選項:

       查考其幫助 valgrind -h       or   info valgrind

 

咱們經過例子看一下它的具體使用。咱們構造一個存在內存泄漏的C程序,以下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

 

 

 

 

 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  

#include  

   

int valgrind_test() 

    int *p = malloc(10 * sizeof(int)); 

   

    p[10] = 0;    

   

    return 0; 

  

   

int main(int argc, const char *argv[]) 

    valgrind_test(); 

    printf("hello world\n"); 

    return 0; 

}

咱們獲得以下錯誤信息:

1340187959_6861.jpg

 

咱們能夠很清楚地看出,分配和釋放了多少內存,有多少內存泄漏。

這對咱們查找內存泄漏十分方便。而後咱們從新編譯程序並綁定調試器:

 

Artistic Style Eclipse Plugin 是一個用來對C/C++代碼進行格式化的 Eclipse 插件,可在 Eclipse CDT 環境中使用.能夠用來定義基本的編碼格式風格,好比代碼縮進,在* +等運算符兩端加上空格的,不一樣模塊間添加空行

CppNcss複雜度檢查工具,可度量函數級,文件級,工程級的複雜度

 

總結:cppcheck與splint是靜態的代碼檢查工具,valgrind爲動態的代碼檢查工具。主要檢查對象都是內存管理錯誤。valgrind因爲前二者,它獲得更爲豐富準確的信息。可是因爲它提供了大量的信息,對因而否是bug的鑑定更爲困難。ArtisticStyle Eclipse Plugin能在基本的代碼格式風格和規範上起到必定的幫助做用。

相關文章
相關標籤/搜索