静态代码分析工具
1 cppcheck 静态代码分析工具
- 参考博客
使用cppcheck检测代码警告、错误
ubuntu下安装cppcheck
sudo apt-get install cppcheck
- 运行指令
cppcheck *.c --enable=all
- 测试的代码示例
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "add.h"
#include "CUnit/Basic.h"
// A test cppcheck is good work
int main()
{
int sum = 0;
int a;
int i = 0;
char * p = NULL;
char * q = NULL;
int array[5] = {};
printf("This is a hello world test.\n");
printf("sum:%d \n", add(3, 5));
sum = add(3, 10);
p = (char *)malloc(4*sizeof(char));
q = (char *)malloc(4*sizeof(char));
//can not found
strcpy(p, "hello");
free(p);
*p = 'a';
//out of range
for(i = 0; i < 6; i++){
array[i] = i;
}
printf("main exit.\n");
return 0;
}
- 测试结果
文件打开未关闭问题,也能被测试出来。
-
对项目中所有文件进行check
cppcheck ./ # cppcheck 路径