Skip to content

Commit 0dc3cb6

Browse files
Fix #11007 FP nullPointerRedundantCheck with static function pointer (cppcheck-opensource#4051)
1 parent 0d35a60 commit 0dc3cb6

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

lib/checknullpointer.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,13 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown, const Set
182182
return false;
183183

184184
// Dereferencing pointer..
185-
if (parent->isUnaryOp("*") && !addressOf)
186-
return true;
185+
if (parent->isUnaryOp("*")) {
186+
// declaration of function pointer
187+
if (tok->variable() && tok->variable()->nameToken() == tok)
188+
return false;
189+
if (!addressOf)
190+
return true;
191+
}
187192

188193
// array access
189194
if (firstOperand && parent->str() == "[" && !addressOf)

test/testnullpointer.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,15 @@ class TestNullPointer : public TestFixture {
11371137
"}");
11381138
ASSERT_EQUALS("[test.cpp:5]: (error) Null pointer dereference: f\n", errout.str());
11391139

1140+
check("int* g();\n" // #11007
1141+
"int* f() {\n"
1142+
" static int* (*fun)() = 0;\n"
1143+
" if (!fun)\n"
1144+
" fun = g;\n"
1145+
" return fun();\n"
1146+
"}\n");
1147+
ASSERT_EQUALS("", errout.str());
1148+
11401149
// loops..
11411150
check("void f() {\n"
11421151
" int *p = 0;\n"

0 commit comments

Comments
 (0)