Fixed #2681 (False negative: Possible null pointer dereference)

This commit is contained in:
Daniel Marjamäki 2011-07-25 15:45:00 +02:00
parent c1138cf7f9
commit 2c6e4c423a
2 changed files with 4 additions and 4 deletions

View File

@ -201,7 +201,7 @@ void CheckNullPointer::nullPointerAfterLoop()
// TODO: Aren't there false negatives. Shouldn't other loops be handled such as: // TODO: Aren't there false negatives. Shouldn't other loops be handled such as:
// - while ( ! %var% ) // - while ( ! %var% )
// - while ( %var% && .. ) // - while ( %var% && .. )
if (! Token::Match(tok, "while ( %var% )")) if (! Token::Match(tok, "while ( %var% )|&&"))
continue; continue;
// Get variable id for the loop variable // Get variable id for the loop variable
@ -217,7 +217,7 @@ void CheckNullPointer::nullPointerAfterLoop()
const std::string varname(tok->strAt(2)); const std::string varname(tok->strAt(2));
// Locate the end of the while loop body.. // Locate the end of the while loop body..
const Token *tok2 = tok->tokAt(4)->link(); const Token *tok2 = tok->next()->link()->next()->link();
// Check if the variable is dereferenced after the while loop // Check if the variable is dereferenced after the while loop
while (0 != (tok2 = tok2 ? tok2->next() : 0)) while (0 != (tok2 = tok2 ? tok2->next() : 0))

View File

@ -88,12 +88,12 @@ private:
check("void foo(const Token *tok)\n" check("void foo(const Token *tok)\n"
"{\n" "{\n"
" while (tok && tok->str() == \"=\")\n" " while (tok && tok->str() == \"=\")\n"
" tok = tok->tokAt(-2);\n" " tok = tok->next();\n"
"\n" "\n"
" if (tok->str() != \";\")\n" " if (tok->str() != \";\")\n"
" ;\n" " ;\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS("[test.cpp:6]: (error) Possible null pointer dereference: tok - otherwise it is redundant to check if tok is null at line 3\n", "", errout.str()); ASSERT_EQUALS("[test.cpp:6]: (error) Possible null pointer dereference: tok - otherwise it is redundant to check if tok is null at line 3\n", errout.str());
check("void foo()\n" check("void foo()\n"
"{\n" "{\n"