栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

cppcheck的安装及基本使用

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

cppcheck的安装及基本使用

本文主要介绍cppcheck这款C/C++源码静态分析工具的安装及基本使用方法。

其github仓库:https://github.com/danmar/cppcheck

官网:https://cppcheck.sourceforge.io/

cppcheck简介

cppcheck的特色是使用unsound 的流敏感的分析。其他的工具基于IR层使用路径敏感的分析,有其优点也有不足。理论上,路径敏感的分析要优于流敏感的分析。但在实际中,cppcheck会检测到其他工具没检测的漏洞。

在cppcheck中,数据流分析不是前向的,而是双向的。比如下面的代码,cppcheck和大多数的分析器一样,都会发现缓冲区溢出。

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

而结合了双向数据流分析的cppcheck也能够发现下面的代码的缓冲区溢出。

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

cppcheck能检测的bug类型,可以在该网址找到:https://sourceforge.net/p/cppcheck/wiki/ListOfChecks/

安装

官网有给出多种系统的安装方式

ubuntu下直接apt安装

sudo apt-get install cppcheck
使用

检测当前文件夹所有的c代码,并将结果保存在err.txt中。

cppcheck . 2> err.txt

打开err.txt,能看到检测的结果如下:

会发现有些小乱,可以指定template模板来指定输出的格式模板。

--template=''  Format the error messages. Available fields:
                           {file}              file name
                           {line}              line number
                           {column}            column number
                           {callstack}         show a callstack. Example:
                                                 [file.c:1] -> [file.c:100]
                           {inconclusive:text} if warning is inconclusive, text
                                               is written
                           {severity}          severity
                           {message}           warning message
                           {id}                warning id
                           {cwe}               CWE id (Common Weakness Enumeration)
                           {code}              show the real code
                           t                 insert tab
                           n                 insert newline
                           r                 insert carriage return
                         Example formats:
                         '{file}:{line},{severity},{id},{message}' or
                         '{file}({line}):({severity}) {message}' or
                         '{callstack} {message}'
                         Pre-defined templates: gcc (default), cppcheck1 (old default), vs, edit.

e.g.

cppcheck . --template="{id} {file}:{line},{severity},{callstack}" 2> err.txt

可以看到打印出的结果的格式变化了,同时对于某些bug,还可以打印出他的callstack。

另外一个有用的选项是enable,可以选择开启哪些checker。

--enable=        Enable additional checks. The available ids are:
                          * all
                                  Enable all checks. It is recommended to only
                                  use --enable=all when the whole program is
                                  scanned, because this enables unusedFunction.
                          * warning
                                  Enable warning messages
                          * style
                                  Enable all coding style checks. All messages
                                  with the severities 'style', 'performance' and
                                  'portability' are enabled.
                          * performance
                                  Enable performance messages
                          * portability
                                  Enable portability messages
                          * information
                                  Enable information messages
                          * unusedFunction
                                  Check for unused functions. It is recommend
                                  to only enable this when the whole program is
                                  scanned.
                          * missingInclude
                                  Warn if there are missing includes. For
                                  detailed information, use '--check-config'.
                         Several ids can be given if you separate them with
                         commas. See also --std

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1037569.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号