Null pointers: Fixed false negative for such code: 'if (p && *p) {} else { *p=0; }'. Ticket: #2379

This commit is contained in:
Daniel Marjamäki 2011-02-20 14:38:49 +01:00
parent 537ac0cb34
commit 7894d86132
2 changed files with 8 additions and 3 deletions

View File

@ -580,8 +580,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
vartok = tok->tokAt(4); vartok = tok->tokAt(4);
else if (Token::Match(tok, "if ( %var% == NULL|0 )|&&")) else if (Token::Match(tok, "if ( %var% == NULL|0 )|&&"))
vartok = tok->tokAt(2); vartok = tok->tokAt(2);
else if (Token::Match(tok, "if|while ( %var% )|&&") && else if (Token::Match(tok, "if|while ( %var% )|&&"))
!Token::simpleMatch(tok->tokAt(4)->link(), "} else"))
vartok = tok->tokAt(2); vartok = tok->tokAt(2);
else else
continue; continue;
@ -601,7 +600,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
// start token = inside the if-body // start token = inside the if-body
const Token *tok1 = tok->next()->link()->tokAt(2); const Token *tok1 = tok->next()->link()->tokAt(2);
if (Token::Match(tok, "if|while ( %var% )")) if (Token::Match(tok, "if|while ( %var% )|&&"))
{ {
// pointer might be null // pointer might be null
null = false; null = false;

View File

@ -830,6 +830,12 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p\n", errout.str());
check("void foo(char *p) {\n"
" if (p && *p == 0) {\n"
" } else { *p = 0; }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p\n", errout.str());
check("void foo(abc *p) {\n" check("void foo(abc *p) {\n"
" if (!p) {\n" " if (!p) {\n"
" }\n" " }\n"