Fixed #2789 (null pointer: problems not detected when pointer is assigned and checked if it's null in a if statement)

This commit is contained in:
Daniel Marjamäki 2011-08-01 07:51:32 +02:00
parent 8e888a0dfb
commit a6c7ae8d51
2 changed files with 10 additions and 0 deletions

View File

@ -636,6 +636,8 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
vartok = tok->tokAt(3);
else if (Token::Match(tok, "if|while ( %var% )|&&"))
vartok = tok->tokAt(2);
else if (Token::Match(tok, "if ( ! ( %var% ="))
vartok = tok->tokAt(4);
else
continue;

View File

@ -1177,6 +1177,14 @@ private:
" return fred->a;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: fred - otherwise it is redundant to check if fred is null at line 2\n", errout.str());
// #2789 - assign and check pointer
check("void f() {\n"
" char *p;\n"
" if (!(p=x())) { }\n"
" *p = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 3\n", errout.str());
}
// Test CheckNullPointer::nullConstantDereference