码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 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
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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

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

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

    安装

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

    ubuntu下直接apt安装

    sudo apt-get install cppcheck
    
    • 1

    使用

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

    cppcheck . 2> err.txt
    
    • 1

    打开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.
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    e.g.

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

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

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

    --enable=<id>        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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  • 相关阅读:
    微店商品详情API接口:获取微店商品主题、价格、销量
    学生个人网页设计作品 学生个人网页模板 简单个人主页成品 个人网页制作 HTML学生个人网站作业设计代做
    一文速学-Pandas多文件批次聚合处理详解+实例代码
    Qt5开发从入门到精通——终章、数据库基本概念(后续篇章升级为 QT常规应用开发)
    【Redis】理论进阶篇------浅谈Redis的缓存穿透和雪崩原理
    域名是什么 有什么用
    【代码随想录】算法训练营 第一天 第一章 数组 Part 1
    mysql数据库的授权访问
    总结三:计算机网络面经
    ruoyi-cloud微服务在Linux中使用docker-compose部署
  • 原文地址:https://blog.csdn.net/u013648063/article/details/126244614
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号