自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

系統(tǒng) OpenHarmony
Cppcheck 是 C/C++ 代碼的靜態(tài)分析工具。它提供獨(dú)特的代碼分析技術(shù)來(lái)檢測(cè)缺陷,不檢查代碼中的語(yǔ)法錯(cuò)誤,只檢查編譯器檢查不出來(lái)的缺陷,并專注于檢測(cè)未定義行為錯(cuò)誤和危險(xiǎn)的編碼結(jié)構(gòu)。

??想了解更多關(guān)于開源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??

??51CTO 開源基礎(chǔ)軟件社區(qū)??

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

前文介紹CodeChecker時(shí),使用到了Cppcheck,我們來(lái)看看這個(gè)工具是什么,如何使用。

1、Cppcheck介紹

Cppcheck 是 C/C++ 代碼的靜態(tài)分析工具。它提供獨(dú)特的代碼分析技術(shù)來(lái)檢測(cè)缺陷,不檢查代碼中的語(yǔ)法錯(cuò)誤,只檢查編譯器檢查不出來(lái)的缺陷,并專注于檢測(cè)未定義行為錯(cuò)誤和危險(xiǎn)的編碼結(jié)構(gòu)。其目標(biāo)是減少誤報(bào)、零誤報(bào),檢查代碼中真正的錯(cuò)誤。Cppcheck旨在能夠分析C / C++代碼,即使它具有非標(biāo)準(zhǔn)語(yǔ)法(在嵌入式項(xiàng)目中很常見)。

Cppcheck既有開源版本,也有具有擴(kuò)展的功能和支持的Cppcheck Premium版本,??梢栽L問(wèn) www.cppchecksolutions.com 以獲取商業(yè)版本的更多信息和購(gòu)買選項(xiàng)。

(1)Cppcheck功能特性

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

(2)Cppcheck特有的分析技術(shù)

使用多個(gè)靜態(tài)分析工具可能是一個(gè)好主意,每個(gè)工具都有獨(dú)特的功能特性。這在研究中已經(jīng)證實(shí)。那么Cppcheck的獨(dú)特之處在哪里?

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

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

Cppcheck還將診斷此問(wèn)題,當(dāng)x等于1000時(shí),賦值時(shí)也會(huì)出現(xiàn)數(shù)組越界。

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

(3)未定義行為Undefined behaviour

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

2、Cppcheck安裝

Cppcheck也可以從各種包管理器安裝;但是,您可能會(huì)得到一個(gè)過(guò)時(shí)的版本。為了獲取更新版本,可以訪問(wèn)https://github.com/danmar/cppcheck進(jìn)行源碼安裝。

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

3、使用入門

第一個(gè)測(cè)試程序,這里有一段簡(jiǎn)單的代碼,我們命名為file1.c。

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

執(zhí)行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.

我們?cè)僭囋嚿厦嬲f(shuō)的例子,保存到file2.c。

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

執(zhí)行cppcheck --enable=all file2.c,輸出如下??梢钥吹贸鲇?個(gè)warning和3個(gè)style問(wèn)題。

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支持檢查文件夾中的所有文件。通常一個(gè)項(xiàng)目會(huì)有許多源文件,如果需要同時(shí)檢查,Cppcheck 可以檢查文件夾中的所有文件.如果 path 是一個(gè)文件夾,cppcheck 將遞歸檢查這個(gè)文件夾中的所有源文件。

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)手動(dòng)檢查文件或使用項(xiàng)目文件

使用 Cppcheck 可以手動(dòng)檢查文件,通過(guò)指定文件/文件夾來(lái)檢查和設(shè)置,或者可以使用一個(gè)工程文件(cmake/visual studio)。

使用項(xiàng)目文件更快,因?yàn)樗恍枰浅I俚呐渲谩?/p>

手動(dòng)檢查文件可以更好的控制分析。

不一定哪種方法會(huì)有最好的結(jié)果,建議嘗試一下,可能會(huì)得到不同的結(jié)果,發(fā)現(xiàn)大多數(shù) bug 需要使用這兩種方法。

4、嚴(yán)重級(jí)別Severities

輸出信息中的嚴(yán)重級(jí)別支持如下幾種:

  • error 錯(cuò)誤
    when code is executed there is either undefined behavior or other error, such as
    a memory leak or resource leak。發(fā)現(xiàn)未定義行為或其他錯(cuò)誤,例如內(nèi)存泄露、資源泄露
  • warning告警
    when code is executed there might be undefined behavior可能有未定義行為
  • style樣式風(fēng)格
    stylistic issues, such as unused functions, redundant code, constness, operator
    precedence, possible mistakes.樣式問(wèn)題,例如未使用行數(shù),冗余代碼,常量性,操作符優(yōu)先級(jí),可能的錯(cuò)誤等
  • 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.這些建議只是基于常識(shí),即使修復(fù)這些消息,也不確定會(huì)得到任何可測(cè)量的性能提升。
  • portability可移植性
    portability warnings. Implementation defined behavior. 64-bit portability. Some
    undefined behavior that probably works “as you want”, etc.可移植性警告。64 位的可移植性,代碼可能在不同的編譯器中運(yùn)行結(jié)果不同。
  • information信息
    configuration problems, which does not relate to the syntactical correctness, but
    the used Cppcheck configuration could be improved.配置問(wèn)題,建議在配置期間僅啟用這些

(1)啟用消息

默認(rèn)情況下,只顯示錯(cuò)誤消息,可以通過(guò) --enable 命令啟用更多檢查。

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

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

cppcheck --enable=style file.c
  • 啟用警告和性能消息:
cppcheck --enable=warning,performance file.c
  • 啟用 unusedFunction 檢查。這不能通過(guò) --enable=style 啟用,因?yàn)椴粫?huì)在庫(kù)中正常工作。
cppcheck --enable=unusedFunction file.c
  • 啟用所有消息:
cppcheck --enable=all

5、常見錯(cuò)誤修改

  • 隱式構(gòu)造問(wèn)題。

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

解決方法:在Slice構(gòu)造函數(shù)前加上explicit,使其必須顯示構(gòu)造,當(dāng)然這種有時(shí)并非必須顯示構(gòu)造。

  • 變量未初始化問(wèn)題。

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

解決方法:在構(gòu)造函數(shù)中加入變量初始值。

  • 變量/函數(shù)未使用問(wèn)題。

示例:(style) Unused variable: output。

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

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

  • raw loop問(wèn)題。

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

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

解決辦法:將循環(huán)便利替換為STL標(biāo)準(zhǔn)庫(kù)算法函數(shù)。

  • 引用傳遞問(wèn)題。

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

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

  • const參數(shù)問(wèn)題。

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

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

??想了解更多關(guān)于開源的內(nèi)容,請(qǐng)?jiān)L問(wèn):??

??51CTO 開源基礎(chǔ)軟件社區(qū)??

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

責(zé)任編輯:jianghua 來(lái)源: 51CTO 開源基礎(chǔ)軟件社區(qū)
相關(guān)推薦

2022-12-12 16:11:47

Clang-Tidy工具

2022-12-13 15:42:56

Clang-Tidy靜態(tài)分析工具

2022-12-08 15:25:10

Clang分析工具CSA

2011-04-11 13:58:09

TCP

2021-06-08 13:56:34

工具靜態(tài)代碼

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

浮動(dòng)靜態(tài)路由

2017-05-10 14:27:29

靜態(tài)代碼漏洞安全

2012-05-22 00:28:21

JavaJava開源開源工具

2016-03-29 14:54:36

2013-10-31 11:08:15

2024-01-08 13:47:00

代碼分析工具

2020-12-22 08:00:00

開發(fā)分析工具

2021-01-05 09:25:27

DockerSemgrep代碼靜態(tài)分析工具

2021-12-27 11:11:30

LLVMSPIR-V后端

2009-11-27 15:13:00

PHP靜態(tài)變量stat

2021-01-04 07:57:07

C++工具代碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)