c++可能的空指针取消引用
我对一些代码运行了 cppcheck 以查找可能的运行时错误。在以下情况下,它报告可能存在空指针取消引用:
Foo* x = ... //defined somewhere
...
Foo* y(x); //possible null pointer dereference.
编辑:更好的示例
for( int i = 0; i < N; i++ )
{
Foo* x( ArrayOfObjsContainingFooPtr[i].FooPtr ); // line 3
if( !x ) // line 4
continue;
}
来自 cppcheck 的错误消息:
[C:\file.cpp:3]:(错误)可能为 null 指针取消引用:x - 否则它 检查 x 是否为空是多余的 第 4 行
但我不明白这是怎么可能的。
I ran cppcheck over some code to look for possible runtime errors. And it is reporting a possible null pointer dereference with the following situation:
Foo* x = ... //defined somewhere
...
Foo* y(x); //possible null pointer dereference.
Edit: Better example
for( int i = 0; i < N; i++ )
{
Foo* x( ArrayOfObjsContainingFooPtr[i].FooPtr ); // line 3
if( !x ) // line 4
continue;
}
Error message from cppcheck:
[C:\file.cpp:3]: (error) Possible null
pointer dereference: x - otherwise it
is redundant to check if x is null at
line 4
But I don't see how this is possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我真的很惊讶你收到这个警告。对我来说,效果恰恰相反。使用从 Linux 源代码编译的 cppcheck 1.46.1。这很好:
现在,使用 this 循环体,根据 cppcheck ,它也“很好”,尽管如果我实际尝试运行它,它会出现段错误,显然:
即使这是“很好”:
这最终会生成“可能的”空指针取消引用。可能,对:
有趣的是,虽然这与前面的示例完全等效,但给出了明确的(不是“可能”)空指针取消引用:
结论:cppcheck 是一个非常有缺陷的工具。
I am really surprised that you got that warning. For me, it works exactly the opposite. Using cppcheck 1.46.1 compiled from sources in Linux. This is fine:
Now, with this loop body it is also "fine" according to cppcheck although it segfaults if I actually try to run it, obviously:
Even this is "fine":
And this finally generates "possible" null pointer dereference. Possible, right:
The funny thing is that this, while being completely equivalent to an earlier example, gives definite (not "possible") null pointer dereference:
The conclusion: cppcheck is a really buggy tool.
考虑以下情况:
但是如果
ptr_foo
被写入程序中的另一个点、另一个文件中怎么办?例如,假设在someotherfile.c
中,您发现:那么当
y(如果
。y
取消引用x
,则调用 x)根据我的经验,静态分析工具往往会报告大量误报,因为它们没有有关程序的任何状态信息。
如果您真的想确保不会遇到空指针引用,您可以尝试以下操作:
Take the following:
But what if
ptr_foo
was written to at another point in the program, in another file? For example, let's say that insomeotherfile.c
you find:Then it is entirely possible that
Foo* x = ptr_foo;
could cause bad mojo, wheny(x)
is called, ify
dereferencesx
.From my experience, static analysis tools tend to report a large number of false positives, because they do not have any state information about the program.
If you really want to make sure you won't run into a null pointer reference, you could try something like:
对 Sergey Tachenov 的帖子进行总结:
cppcheck 现在可以正确检测到这个:
下一个示例也可以正确检测到:
这是 cppcheck 的输出:
即使下一个示例也演示了 Cppcheck 按预期工作:
这是输出:
Just a wrap up to the post from Sergey Tachenov:
This one is now correctly detected by cppcheck:
Also the next example is detected correctly:
Here is the output from cppcheck:
Even the next example demonstrates that Cppcheck works as expected:
Here is the output: