cppcheck介绍和安装

CppCheck是一个C/C++代码缺陷静态检查工具。静态代码检查是检查代码是否安全和健壮,是否有隐藏问题。
CppCheck只检查编译器检查不出来的bug,不检查语法错误。

CentOS在线安装命令:
yum install cppcheck

C/C++常见内存问题

(1)heap use after free 堆内存释放后继续使用

(2)stack use after return 栈内存函数返回后继续使用

(3)stack use after scope 栈内存在作用域范围外继续使用

(4)heap buffer overflow 堆内存溢出

(5)stack buffer overflow 栈内存溢出

(6)global buffer overflow 全局内存溢出

(7)allocation size too big 堆内存分配较大

(8)memory leaks 内存泄露

(9)double free 堆内存重复释放

(10)initialization order bugs 初始化命令错误

cppcheck检查内存问题

(1)heap use after free 堆内存释放后继续使用

#include <stdio.h>
int main (int argc, char* argv []) {int* array = new int[100];delete [] array;int num = array[0];return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:5:15: error: Dereferencing 'array' after it is deallocated / released [deallocuse]int num = array[0];^
main.cpp:4:15: error: Memory is allocated but not initialized: array [uninitdata]delete [] array;^

(2)stack use after return 栈内存函数返回后继续使用

#include <stdio.h>
int* p = NULL;
void fun() {int a[10];p = a; // 或者 p = &a[0];
}
int main() {fun();int num = p[0];return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:5:5: error: Non-local variable 'p' will use pointer to local variable 'a'. [danglingLifetime]p = a; // 或者 p = &a[0];^
main.cpp:5:9: note: Array decayed to pointer here.p = a; // 或者 p = &a[0];^
main.cpp:4:9: note: Variable created here.int a[10];^
main.cpp:5:5: note: Non-local variable 'p' will use pointer to local variable 'a'.p = a; // 或者 p = &a[0];^

(3)stack use after scope 栈内存在作用域范围外继续使用

#include <stdio.h>
int* p = NULL;
int main(int argc, char* argv[]) {{int a[10];p = &a[0];}p[0] = 1000;return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:6:9: error: Non-local variable 'p' will use pointer to local variable 'a'. [danglingLifetime]p = &a[0];^
main.cpp:6:13: note: Address of variable taken here.p = &a[0];^
main.cpp:5:13: note: Variable created here.int a[10];^
main.cpp:6:9: note: Non-local variable 'p' will use pointer to local variable 'a'.p = &a[0];^
main.cpp:8:5: error: Using pointer to local variable 'a' that is out of scope. [invalidLifetime]p[0] = 1000;^
main.cpp:6:13: note: Address of variable taken here.p = &a[0];^
main.cpp:5:13: note: Variable created here.int a[10];^
main.cpp:8:5: note: Using pointer to local variable 'a' that is out of scope.p[0] = 1000;^

(4)heap buffer overflow 堆内存溢出

#include <stdio.h>
int main (int argc, char** argv) {int* array = new int[100];int res = array[100];delete [] array;return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:4:15: error: Memory is allocated but not initialized: array [uninitdata]int res = array[100];^

(5)stack buffer overflow 栈内存溢出

#include <stdio.h>
int main(int argc, char* argv[]) {int a[2] = {100,200};int b = a[2];return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:4:14: error: Array 'a[2]' accessed at index 2, which is out of bounds. [arrayIndexOutOfBounds]int b = a[2];^

(6)global buffer overflow 全局内存溢出

#include <stdio.h>
int array[100];
int main(int argc, char* argv[]) {int num = array[100];return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:4:20: error: Array 'array[100]' accessed at index 100, which is out of bounds. [arrayIndexOutOfBounds]int num = array[100];^

(7)allocation size too big 堆内存分配较大

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {int x = 1000;int y = 1000;int* p = (int*)malloc(x * y * x * y);free(p);return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:7:37: error: Signed integer overflow for expression 'x*y*x*y'. [integerOverflow]int* p = (int*)malloc(x * y * x * y);^
main.cpp:5:13: note: Assignment 'x=1000', assigned value is 1000int x = 1000;^
main.cpp:7:37: note: Integer overflowint* p = (int*)malloc(x * y * x * y);^

(8)memory leaks 内存泄露

#include <stdlib.h>
int main(int argc, char *argv[]) {int* ptr = (int*)malloc(10 * sizeof(int));int num = ptr[0];return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:5:5: error: Memory leak: ptr [memleak]return 0;^
main.cpp:4:15: error: Memory is allocated but not initialized: ptr [uninitdata]int num = ptr[0];^

(9)double free 堆内存重复释放

#include <stdio.h>
int main(int argc, char* argv[]) {int* p = new int[10];delete [] p;delete [] p;return 0;
}

cppcheck main.cpp

Checking main.cpp ...
main.cpp:5:15: error: Memory pointed to by 'p' is freed twice. [doubleFree]delete [] p;^
main.cpp:4:5: note: Memory pointed to by 'p' is freed twice.delete [] p;^
main.cpp:5:15: note: Memory pointed to by 'p' is freed twice.delete [] p;^
main.cpp:4:15: error: Memory is allocated but not initialized: p [uninitdata]delete [] p;^

(10)initialization order bugs 初始化命令错误

暂时还没有找到能够检测出来的案例。

C/C++代码缺陷静态检查工具cppcheck相关推荐

  1. 代码质量静态检查工具

    一 点睛 代码质量静态检查工具可以自动快速发现劣质代码,潜在Bug,给出代码优化建议.因此代码静态检查工具在实际项目研发中有举足轻重的作用,利用好各种优秀检查工具是做好品质管理的重要环节. 二 静态分 ...

  2. 静态代码检查工具 cppcheck 的使用

    CppCheck是一个C/C++代码缺陷静态检查工具.不同于C/C++编译器及其它分析工具,CppCheck只检查编译器检查不出来的bug,不检查语法错误.所谓静态代码检查就是使用一个工具检查我们写的 ...

  3. Linux开发者的CI/CD(8)静态代码检查工具cppcheck使用大全

    文章目录 1.简述 2.使用大全 3.生成可视化报告 翻译自官方手册:https://cppcheck.sourceforge.io/manual.pdf 1.简述 cppcheck 是一种 C/C+ ...

  4. cppcheck 自定义规则_cppcheck代码静态检查工具及相关工具插件用法介绍

    摘要:介绍代码缺陷静态检查工具(static code analyzer)cppcheck,以及其vs.qtcreator.git.jenkins插件及用法. Cppcheck着重于检测未定义的行为和 ...

  5. 代码静态检测工具cppcheck简介

    cppcheck是一款小型的代码缺陷静态检查工具,支持C.C++代码,作为编译器的一种补充检查方式,不同于C/C++编译器等其他众多分析工具,它不检测代码的语法错误,只检查编译器检查不出来的BUG类型 ...

  6. Jenkins 在 Tomcat 中的部署及代码静态检查工具集成

    Jenkins 的简单部署 在安装了 Jenkins 运行所需的依赖(主要是 JDK)之后,可以通过如下步骤简单快速地部署 Jenkins: 下载 Jenkins. 打开终端并切换至下载目录. 运行命 ...

  7. 一些代码静态检查工具的简介

    1.KLOCWORK: 适用语言:C, C++, JAVA 是否开源:否, 是否需要编译:是 作用:代码静态检查工具.用于高效检测软件缺陷和安全隐患,提供优秀的静态源代码分析解决方案.软件号称是业界领 ...

  8. 代码静态检查工具PC-Lint运用实践

    代码静态检查工具PC-Lint运用实践 如何提交zero bug的产品,如何尽早发现bug,是软件开发工程师和测试工程师都需要思考的问题.我认为高质量的代码是关键,具体实施保障办法有:框架约束,代码评 ...

  9. 从静态检查工具谈代码编程规范

    提升自身编程能力是每一个码农的追求,也许我们每日都可以写上几百几千行代码,抑或每日只能修修补补添加几行代码,可是编写代码的行数的增加确实是提升编程能力的体现吗?从我个人的理解来看并不是这样. 从学校到 ...

最新文章

  1. IAR编译提示could not open file lnkstm8s003f3.icf
  2. 影响网站快照异常的因素有哪些?
  3. Struts2 注解中跳转 action
  4. flask jinja2 mysql_flask/jinja2 SSTI注入学习
  5. javascript设计模式系列 - LukeLin - 博客园
  6. 基于C语言Ncurse库和链表的简单贪吃蛇小游戏
  7. UVA - 725 Division-sprintf的妙用
  8. Oracle管理文件OMF (oracle managed files)
  9. 【赛尔笔记】文本摘要论文列表
  10. 【论文】如何下载SCI论文
  11. linux IIC子系统分析(七)——实例分析通过i2c-dev操作I2C设备
  12. 安卓实现播放器app
  13. 计算机usb无法读取u盘启动,U盘无法识别的USB设备怎么办解决
  14. JAVA计算机毕业设计在线招投标系统Mybatis+系统+数据库+调试部署
  15. Mockjs-官网学习总结
  16. 那些移动端web踩过的坑2
  17. linux堆内存管理
  18. 进行范围查找的折半查找法
  19. context 浅析
  20. vue中下载文件导出保存到本地

热门文章

  1. 第一章 开启网络安全态势感知的旅程
  2. Android 5.1双卡打电话发短信的实际应用
  3. 中国/玩具鸭舰队/漂流15年 今夏将抵英国
  4. 3D打印机的使用教程
  5. 一文搞懂测试左移和测试右移
  6. PHP框架之ThinkPHP
  7. tensorflow查看pb文件
  8. AI模型看看视频,就学会了玩《我的世界》:砍树、造箱子、制作石镐样样不差...
  9. Mathcad使用数学表达式
  10. Android 权限(一):权限大全