Null pointer: fix false negatives when unknown function is called and pointer is local/argument

This commit is contained in:
Daniel Marjamäki 2011-07-31 17:32:25 +02:00
parent cc89687e8c
commit 1640f3d1ca
2 changed files with 17 additions and 1 deletions

View File

@ -735,7 +735,15 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
(Token::Match(tok2->link()->tokAt(-2), "[;{}] %var% (") ||
Token::Match(tok2->link()->tokAt(-5), "[;{}] ( * %var% ) (")))
{
break;
// noreturn function?
if (tok2->strAt(2) == "}")
break;
// init function (global variables)
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
const Variable *var = symbolDatabase->getVariableFromVarId(varid);
if (!var || !(var->isLocal() || var->isArgument()))
break;
}
if (tok2->varId() == varid)

View File

@ -992,6 +992,14 @@ private:
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str());
check("void foo(char *p) {\n"
" if (p) {\n"
" }\n"
" bar();\n"
" strcpy(p, \"abc\");\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str());
check("void foo(abc *p) {\n"
" if (!p) {\n"
" }\n"