成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

靜態分析工具Clang Static Analyzer (3) Cppcheck

系統 OpenHarmony
Cppcheck 是 C/C++ 代碼的靜態分析工具。它提供獨特的代碼分析技術來檢測缺陷,不檢查代碼中的語法錯誤,只檢查編譯器檢查不出來的缺陷,并專注于檢測未定義行為錯誤和危險的編碼結構。

??想了解更多關于開源的內容,請訪問:??

??51CTO 開源基礎軟件社區??

??https://ost.51cto.com??

前文介紹CodeChecker時,使用到了Cppcheck,我們來看看這個工具是什么,如何使用。

1、Cppcheck介紹

Cppcheck 是 C/C++ 代碼的靜態分析工具。它提供獨特的代碼分析技術來檢測缺陷,不檢查代碼中的語法錯誤,只檢查編譯器檢查不出來的缺陷,并專注于檢測未定義行為錯誤和危險的編碼結構。其目標是減少誤報、零誤報,檢查代碼中真正的錯誤。Cppcheck旨在能夠分析C / C++代碼,即使它具有非標準語法(在嵌入式項目中很常見)。

Cppcheck既有開源版本,也有具有擴展的功能和支持的Cppcheck Premium版本,。可以訪問 www.cppchecksolutions.com 以獲取商業版本的更多信息和購買選項。

(1)Cppcheck功能特性

  • 獨特的代碼分析,可檢測代碼中的各種錯誤。
  • 命令行界面和圖形用戶界面都可用。
  • Cppcheck非常注重檢測未定義的行為。

(2)Cppcheck特有的分析技術

使用多個靜態分析工具可能是一個好主意,每個工具都有獨特的功能特性。這在研究中已經證實。那么Cppcheck的獨特之處在哪里?

Cppcheck使用不健全的流程敏感分析,其他幾種分析器使用基于抽象解釋的路徑敏感分析,這也很好,但既有優點也有缺點。從理論上講,根據定義,路徑敏感分析比流量敏感分析更好。但實際上,這意味著Cppcheck將檢測其他工具無法檢測到的錯誤。在Cppcheck中,數據流分析不僅是“前向”的,而且是“雙向的”。大多數分析器會診斷這一點,可以確定數組索引為 1000 時會出現溢出。

void foo(int x)
{
int buf[10];
if (x == 1000)
buf[x] = 0; // <- ERROR
}

Cppcheck還將診斷此問題,當x等于1000時,賦值時也會出現數組越界。

void foo(int x)
{
int buf[10];
buf[x] = 0; // <- ERROR
if (x == 1000) {}
}

(3)未定義行為Undefined behaviour

  • Dead pointers 死指針
  • Division by zero 除以零
  • Integer overflows整數溢出
  • Invalid bit shift operands無效的位移操作數
  • Invalid conversions無效轉化
  • Invalid usage of STLSTL 的用法無效
  • Memory management內存管理
  • Null pointer dereferences空指針解引用
  • Out of bounds checking越界檢查
  • Uninitialized variables未初始化的變量
  • Writing const data寫入常量數據

2、Cppcheck安裝

Cppcheck也可以從各種包管理器安裝;但是,您可能會得到一個過時的版本。為了獲取更新版本,可以訪問https://github.com/danmar/cppcheck進行源碼安裝。

  • Debian:
sudo apt-get install cppcheck
  • Fedora:
sudo yum install cppcheck
  • macOS:
brew install cppcheck

3、使用入門

第一個測試程序,這里有一段簡單的代碼,我們命名為file1.c。

int main()
{
char a[10];
a[10] = 0;
return 0;
}

執行cppcheck file1.c,輸出如下:

zhushangyuan@DESKTOP-RPE9R4O:~/CSA$ cppcheck file1.c
Checking file1.c ...
[file1.c:4]: (error) Array 'a[10]' accessed at index 10, which is out of bounds.

我們再試試上面說的例子,保存到file2.c。

void foo(int x)
{
int buf[10];
buf[x] = 0; // <- ERROR 1
if (x == 1000) {
buf[x] = 0; // <- ERROR 2
}
}

執行cppcheck --enable=all file2.c,輸出如下。可以看得出有2個warning和3個style問題。

zhushangyuan@DESKTOP-RPE9R4O:~/CSA$ cppcheck --enable=all file2.c
Checking file2.c ...
file2.c:4:6: warning: Either the condition 'x==1000' is redundant or the array 'buf[10]' is accessed at index 1000, which is out of bounds. [arrayIndexOutOfBoundsCond]
buf[x] = 0; // <- ERROR 1
^
file2.c:5:9: note: Assuming that condition 'x==1000' is not redundant
if (x == 1000) {
^
file2.c:4:6: note: Array index out of bounds
buf[x] = 0; // <- ERROR 1
^
file2.c:6:8: warning: Either the condition 'x==1000' is redundant or the array 'buf[10]' is accessed at index 1000, which is out of bounds. [arrayIndexOutOfBoundsCond]
buf[x] = 0; // <- ERROR 2
^
file2.c:5:9: note: Assuming that condition 'x==1000' is not redundant
if (x == 1000) {
^
file2.c:6:8: note: Array index out of bounds
buf[x] = 0; // <- ERROR 2
^
file2.c:4:10: style: Variable 'buf[x]' is assigned a value that is never used. [unreadVariable]
buf[x] = 0; // <- ERROR 1
^
file2.c:6:12: style: Variable 'buf[x]' is assigned a value that is never used. [unreadVariable]
buf[x] = 0; // <- ERROR 2
^
file2.c:1:0: style: The function 'foo' is never used. [unusedFunction]

^

(1)檢查文件夾

Cppcheck支持檢查文件夾中的所有文件。通常一個項目會有許多源文件,如果需要同時檢查,Cppcheck 可以檢查文件夾中的所有文件.如果 path 是一個文件夾,cppcheck 將遞歸檢查這個文件夾中的所有源文件。

cppcheck path

示例輸出如下:

zhushangyuan@DESKTOP-RPE9R4O:~/CSA$ cppcheck .
Checking file1.c ...
file1.c:4:4: error: Array 'a[10]' accessed at index 10, which is out of bounds. [arrayIndexOutOfBounds]
a[10] = 0;
^
1/4 files checked 12% done
Checking file2.c ...
2/4 files checked 38% done
Checking hello.c ...
hello.c:2:13: error: Division by zero. [zerodiv]
int x = 7 / 0; // bug here
^
3/4 files checked 50% done
Checking simple.c ...
simple.c:16:11: error: Division by zero. [zerodiv]
return 5/(x-x); // warn
^
simple.c:12:5: error: Uninitialized variable: s [uninitvar]
f(s); // warn
^
simple.c:12:5: error: Uninitialized struct member: s.x [uninitStructMember]
f(s); // warn
^
4/4 files checked 100% done

(2)手動檢查文件或使用項目文件

使用 Cppcheck 可以手動檢查文件,通過指定文件/文件夾來檢查和設置,或者可以使用一個工程文件(cmake/visual studio)。

使用項目文件更快,因為它只需要非常少的配置。

手動檢查文件可以更好的控制分析。

不一定哪種方法會有最好的結果,建議嘗試一下,可能會得到不同的結果,發現大多數 bug 需要使用這兩種方法。

4、嚴重級別Severities

輸出信息中的嚴重級別支持如下幾種:

  • error 錯誤
    when code is executed there is either undefined behavior or other error, such as
    a memory leak or resource leak。發現未定義行為或其他錯誤,例如內存泄露、資源泄露
  • warning告警
    when code is executed there might be undefined behavior可能有未定義行為
  • style樣式風格
    stylistic issues, such as unused functions, redundant code, constness, operator
    precedence, possible mistakes.樣式問題,例如未使用行數,冗余代碼,常量性,操作符優先級,可能的錯誤等
  • performance性能
    run time performance suggestions based on common knowledge, though it is
    not certain any measurable speed difference will be achieved by fixing these
    messages.這些建議只是基于常識,即使修復這些消息,也不確定會得到任何可測量的性能提升。
  • portability可移植性
    portability warnings. Implementation defined behavior. 64-bit portability. Some
    undefined behavior that probably works “as you want”, etc.可移植性警告。64 位的可移植性,代碼可能在不同的編譯器中運行結果不同。
  • information信息
    configuration problems, which does not relate to the syntactical correctness, but
    the used Cppcheck configuration could be improved.配置問題,建議在配置期間僅啟用這些

(1)啟用消息

默認情況下,只顯示錯誤消息,可以通過 --enable 命令啟用更多檢查。

  • 啟用警告消息:
cppcheck --enable=warning file.c
  • 啟用性能消息:
cppcheck --enable=performance file.c
  • 啟用信息消息:
cppcheck --enable=information file.c

由于歷史原因 --enable=style 可以啟用警告、性能、可移植性和樣式信息。當使用舊 XML 格式時,這些都由 style 表示:

cppcheck --enable=style file.c
  • 啟用警告和性能消息:
cppcheck --enable=warning,performance file.c
  • 啟用 unusedFunction 檢查。這不能通過 --enable=style 啟用,因為不會在庫中正常工作。
cppcheck --enable=unusedFunction file.c
  • 啟用所有消息:
cppcheck --enable=all

5、常見錯誤修改

  • 隱式構造問題。

示例: (style) Class ‘Slice’ has a constructor with 1 argument that is not explicit。

解決方法:在Slice構造函數前加上explicit,使其必須顯示構造,當然這種有時并非必須顯示構造。

  • 變量未初始化問題。

示例:(warning) Member variable ‘TableFileCreationInfo::file_size’ is not initialized in the constructor.

解決方法:在構造函數中加入變量初始值。

  • 變量/函數未使用問題。

示例:(style) Unused variable: output。

示例:(style) The function ‘rocksmt_wal_iter_status’ is never used。

解決方法:考慮后期是否還需要,不需要的及時刪除,需要的保留。

  • raw loop問題。

示例:(style) Consider using std::fill algorithm instead of a raw loop. [useStlAlgorithm]。

示例:(style) Consider using std::transform algorithm instead of a raw loop. [useStlAlgorithm]。

解決辦法:將循環便利替換為STL標準庫算法函數。

  • 引用傳遞問題。

示例:(performance) Function parameter ‘f’ should be passed by reference.

解決辦法:在聲明function時,能用引用傳遞的盡量使用引用傳遞,盡量避免值傳遞。

  • const參數問題。

示例:(performance) Function parameter ‘s’ should be passed by const reference. [passedByValue]。

解決辦法:形參s前加上const,在函數中未被修改的變量,盡量聲明為const。

??想了解更多關于開源的內容,請訪問:??

??51CTO 開源基礎軟件社區??

??https://ost.51cto.com??。

責任編輯:jianghua 來源: 51CTO 開源基礎軟件社區
相關推薦

2022-12-12 16:11:47

Clang-Tidy工具

2022-12-13 15:42:56

Clang-Tidy靜態分析工具

2022-12-08 15:25:10

Clang分析工具CSA

2011-04-11 13:58:09

TCP

2021-06-08 13:56:34

工具靜態代碼

2017-04-19 12:05:59

2021-07-29 06:37:55

KubernetesKubeLinter工具

2016-05-17 13:54:05

2010-08-20 15:07:22

浮動靜態路由

2012-05-22 00:28:21

JavaJava開源開源工具

2017-05-10 14:27:29

靜態代碼漏洞安全

2016-03-29 14:54:36

2013-10-31 11:08:15

2021-12-27 11:11:30

LLVMSPIR-V后端

2024-01-08 13:47:00

代碼分析工具

2021-01-05 09:25:27

DockerSemgrep代碼靜態分析工具

2020-12-22 08:00:00

開發分析工具

2021-01-04 07:57:07

C++工具代碼

2009-11-27 15:13:00

PHP靜態變量stat
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 91一区二区| 成人精品鲁一区一区二区 | 精品在线一区 | 成人免费观看男女羞羞视频 | 久久久精品一区 | 国产综合在线视频 | 亚洲不卡在线视频 | 操网站 | 国产一区二区三区四区五区加勒比 | 久久久91精品国产一区二区三区 | 国产亚洲精品一区二区三区 | h视频在线观看免费 | 亚洲欧美综合 | 九九精品在线 | h片免费在线观看 | 国产福利在线视频 | 日本视频一区二区三区 | 中文字幕蜜臀 | 国产欧美精品区一区二区三区 | 亚洲三区在线 | 免费黄色大片 | 午夜精品视频 | 免费av观看| 色姑娘综合网 | 成人免费一区二区三区视频网站 | 亚洲嫩草| www天天操| 天天干天天玩天天操 | 精品国产乱码久久久久久丨区2区 | 日韩精品成人 | 亚洲欧洲综合av | 久久网日本| 精品国产视频在线观看 | 国产一区二区 | 日韩精品视频一区二区三区 | 欧美婷婷| 国产福利资源在线 | 久久久www成人免费精品张筱雨 | 亚洲二区视频 | 日本精品视频一区二区 | 日韩高清中文字幕 |