Skip to content

Commit 309edbb

Browse files
committed
Fixed cppcheck-opensource#4389 (False positive: Possible null pointer dereference if the default parameter value is used)
1 parent 6bd7463 commit 309edbb

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎lib/checknullpointer.cpp‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,12 @@ void CheckNullPointer::nullPointerDefaultArgument()
12231223
}
12241224
}
12251225
if (dependsOnPointer && Token::simpleMatch(endOfCondition, ") {")) {
1226-
tok = endOfCondition->next()->link();
1226+
const Token *endOfIf = endOfCondition->next()->link();
1227+
for (; tok != endOfIf; tok = tok->next()) {
1228+
// If a pointer is assigned a new value, stop considering it.
1229+
if (Token::Match(tok, "%var% ="))
1230+
pointerArgs.erase(tok->varId());
1231+
}
12271232
continue;
12281233
}
12291234
}
@@ -1232,7 +1237,7 @@ void CheckNullPointer::nullPointerDefaultArgument()
12321237
continue;
12331238

12341239
// If a pointer is assigned a new value, stop considering it.
1235-
if (Token::Match(tok, "%var% = %any%"))
1240+
if (Token::Match(tok, "%var% ="))
12361241
pointerArgs.erase(tok->varId());
12371242

12381243
if (isPointerDeRef(tok, unknown, symbolDatabase))

‎test/testnullpointer.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2193,6 +2193,14 @@ class TestNullPointer : public TestFixture {
21932193
"}");
21942194
ASSERT_EQUALS("", errout.str());
21952195

2196+
check("void f(int *p = 0) {\n"
2197+
" int y;\n"
2198+
" if (p == 0)\n"
2199+
" p = &y;\n"
2200+
" *p = 0;\n"
2201+
"}");
2202+
ASSERT_EQUALS("", errout.str());
2203+
21962204
check("void f(int *p = 0) {\n"
21972205
" if (a != 0)\n"
21982206
" *p = 0;\n"

0 commit comments

Comments
 (0)