Skip to content

Commit 079d22f

Browse files
committed
Fixed cppcheck-opensource#4734 (False 'Possible null pointer dereference')
1 parent de8ee5b commit 079d22f

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

lib/checknullpointer.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,19 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
762762
// Variable id for pointer
763763
const unsigned int varid(vartok->varId());
764764

765+
// bailout for while scope if pointer is assigned inside the loop
766+
if (i->type == Scope::eWhile) {
767+
bool assign = false;
768+
for (const Token *tok2 = i->classStart; tok2 && tok2 != i->classEnd; tok2 = tok2->next()) {
769+
if (Token::Match(tok2, "%varid% =", varid)) {
770+
assign = true;
771+
break;
772+
}
773+
}
774+
if (assign)
775+
continue;
776+
}
777+
765778
// Name of pointer
766779
const std::string& varname(vartok->str());
767780

test/testnullpointer.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,19 @@ class TestNullPointer : public TestFixture {
587587
"}");
588588
TODO_ASSERT_EQUALS("error", "", errout.str());
589589

590+
// while
591+
check("void f(int *p) {\n"
592+
" *p = 0;\n"
593+
" while (p) { p = 0; }\n"
594+
"}");
595+
ASSERT_EQUALS("", errout.str());
596+
597+
check("void f(int *p) {\n"
598+
" *p = 0;\n"
599+
" while (p) { }\n"
600+
"}");
601+
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str());
602+
590603
// Ticket #3125
591604
check("void foo(ABC *p)\n"
592605
"{\n"

0 commit comments

Comments
 (0)