最近与一开源社区交流,了解到它的项目采用cppcheck
做一项代码检查。
最近一直在试用cppcheck
,看其效果。直到今天看到一个逻辑大致正确,但存在笔误的代码,觉得cppcheck
对程序员还是有很大辅助价值的!
#include
#include
#include
#include
int main(void)
{
int a = 1;
int b = 2;
printf("The third parameter is inlvaid, Can GCC report warning?\n");
if(memcmp(&a, &b, sizeof(a) != 0))
{
printf("Hello memcmp sizeof(int)\n");
}
int c[16] = {};
int d[16] = {};
if(memcmp(&c, &d, sizeof(c) != 0))
{
printf("Hello memcmp sizeof(int[16])\n");
}
int e[16] = {};
int f[16] = {};
if(memcmp(&e, &f, sizeof(e)) != 0)
{
printf("Hello memcmp sizeof(int[16]) with valid parameter\n");
}
printf("Exit from program!\n");
return 0;
}
这里存在一个明显的笔误,GCC 7 or 9
都没有上报出来,用cppcheck
工具就可以将存在隐患的问题上报出来
cppcheck main.c
Checking main.c ...
main.c:13:31: error: Invalid memcmp() argument nr 3. A non-boolean value is required. [invalidFunctionArgBool]
if(memcmp(&a, &b, sizeof(a) != 0))
通过查看样例汇编代码,如果比较的是4个字节的信息,很有可能笔误,还是能够歪打正着的