Null pointer: better handling when calling standard functions with possible null pointer

This commit is contained in:
Daniel Marjamäki 2011-07-31 17:19:23 +02:00
parent afc825da68
commit cc89687e8c
2 changed files with 22 additions and 0 deletions

View File

@ -715,6 +715,21 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
continue; continue;
} }
// function call, check if pointer is dereferenced
if (Token::Match(tok2, "%var% ("))
{
std::list<const Token *> var;
parseFunctionCall(*tok2, var, 0);
for (std::list<const Token *>::const_iterator it = var.begin(); it != var.end(); ++it)
{
if ((*it)->varId() == varid)
{
nullPointerError(*it, pointerName, linenr);
break;
}
}
}
// calling unknown function (abort/init).. // calling unknown function (abort/init)..
if (Token::simpleMatch(tok2, ") ;") && if (Token::simpleMatch(tok2, ") ;") &&
(Token::Match(tok2->link()->tokAt(-2), "[;{}] %var% (") || (Token::Match(tok2->link()->tokAt(-2), "[;{}] %var% (") ||

View File

@ -985,6 +985,13 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (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"
" strcpy(p, \"abc\");\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 2\n", errout.str());
check("void foo(abc *p) {\n" check("void foo(abc *p) {\n"
" if (!p) {\n" " if (!p) {\n"
" }\n" " }\n"