null pointer: fixed false positive when condition checks if pointer is ok

This commit is contained in:
Daniel Marjamäki 2009-10-30 15:14:24 +01:00
parent feb065abc8
commit 7dbf22aa3f
2 changed files with 25 additions and 0 deletions

View File

@ -1129,6 +1129,22 @@ void CheckOther::nullPointerConditionalAssignment()
tok3 = tok3->next()->link(); tok3 = tok3->next()->link();
if (!tok3) if (!tok3)
break; break;
// check if the condition contains the variable..
for (const Token *tok4 = tok2->tokAt(2); tok4 && tok4 != tok3; tok4 = tok4->next())
{
if (tok4->varId() == varid)
{
// The condition contains the variable..
// to avoid false positives I bail out..
tok3 = 0;
break;
}
}
if (!tok3)
break;
if (tok3->next()->str() == "{") if (tok3->next()->str() == "{")
{ {
tok2 = tok3->next()->link(); tok2 = tok3->next()->link();

View File

@ -910,6 +910,15 @@ private:
" p->abcd();\n" " p->abcd();\n"
"}\n"); "}\n");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
checkNullPointer("static void foo()\n"
"{\n"
" Foo *p = 0;\n"
" if (!p)\n"
" return;\n"
" p->abcd();\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }