Skip to content

Commit bb8a593

Browse files
Fix #13220 FN nullPointerRedundantCheck with constructor (regression) (danmar#8132)
1 parent fabe149 commit bb8a593

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/checknullpointer.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ void CheckNullPointer::nullPointerByDeRefAndCheck()
310310

311311
return true;
312312
};
313-
std::vector<const Token *> tokens = findTokensSkipDeadAndUnevaluatedCode(mSettings->library, scope->bodyStart, scope->bodyEnd, pred);
313+
const Token* const start = (scope->function && scope->function->isConstructor()) ? scope->function->token : scope->bodyStart; // Check initialization list
314+
std::vector<const Token *> tokens = findTokensSkipDeadAndUnevaluatedCode(mSettings->library, start, scope->bodyEnd, pred);
314315
for (const Token *tok : tokens) {
315316
const ValueFlow::Value *value = tok->getValue(0);
316317

test/testnullpointer.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3742,6 +3742,15 @@ class TestNullPointer : public TestFixture {
37423742
" return 1 ? 1 : *(int*)0 = 1;\n"
37433743
"}\n");
37443744
ASSERT_EQUALS("", errout_str());
3745+
3746+
check ("struct S {\n" // #13220
3747+
" explicit S(int* p) : i(*p) {\n"
3748+
" if (p) {}\n"
3749+
" }\n"
3750+
" int i;\n"
3751+
"};\n");
3752+
ASSERT_EQUALS("[test.cpp:3:13] -> [test.cpp:2:29]: (warning) Either the condition 'p' is redundant or there is possible null pointer dereference: p. [nullPointerRedundantCheck]\n",
3753+
errout_str());
37453754
}
37463755

37473756
void nullpointerDelete() {

0 commit comments

Comments
 (0)