cppcheck取消引用空指针

问题描述 投票:1回答:1

我知道cppcheck可以检查对变量的空指针的取消引用。例如,这将触发cpp检查:

int* a = NULL;
*a = 5;

是否可以配置cppcheck以便同时验证函数返回的指针?像这样的东西:

int* foo() { return NULL; }
void main()
{
  int a = *foo();
}

而且,如果可能的话,它也可以在智能指针上工作吗?

c++ static-code-analysis cppcheck
1个回答
2
投票

使用最新版本的Cppcheck(https://github.com/danmar/cppcheck/commit/e6d692d9605850cf98153a4825e353898b9320a2)运行示例提供

$ g++ -c nullPointer.cpp && cppcheck --enable=all --inconclusive nullPointer.cpp
Checking nullPointer.cpp ...
nullPointer.cpp:4:14: error: Null pointer dereference: foo() [nullPointer]
 int a = *foo();
             ^
nullPointer.cpp:2:0: style: The function 'f' is never used. [unusedFunction]

$ more nullPointer.cpp
int * foo(void) {return 0;}
int f(void)
{
    int a = *foo();
    return a;
}

您使用的是哪个版本?

更新:同时向Cppchecks单元测试套件中添加了一个测试:https://github.com/danmar/cppcheck/commit/fd900ab8b249eec37df706f338c227f2a9adb3ef

© www.soinside.com 2019 - 2024. All rights reserved.