null pointers: fixed false positives (#49)

This commit is contained in:
Daniel Marjamäki 2009-07-23 19:02:14 +02:00
parent 565ac2fca6
commit 06c8ff9d0d
2 changed files with 22 additions and 1 deletions

View File

@ -1090,11 +1090,15 @@ void CheckOther::nullPointer()
{
if (tok1->varId() == varid)
{
if (tok1->previous() && tok1->previous()->str() == "*")
if (tok1->previous() && tok1->previous()->str() == "*" && !Token::simpleMatch(tok1->tokAt(-2), "*"))
{
nullPointerError(tok1);
break;
}
else if (tok1->previous() && tok1->previous()->str() == "&")
{
break;
}
else if (tok1->next() && tok1->next()->str() == "=")
{
break;

View File

@ -523,6 +523,23 @@ private:
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference\n", errout.str());
// no error
checkNullPointer("void foo()\n"
"{\n"
" int *p;\n"
" f(&p);\n"
" if (!p)\n"
" ;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkNullPointer("void foo()\n"
"{\n"
" int **p = f();\n"
" if (!p)\n"
" ;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkNullPointer("void foo(int *p)\n"
"{\n"
" if (x)\n"