Fixed #3125 (FP: Possible null pointer dereference in conditional operator)

This commit is contained in:
Daniel Marjamäki 2011-10-02 19:27:18 +02:00
parent b2d0e48a31
commit 480c403511
2 changed files with 41 additions and 1 deletions

View File

@ -589,8 +589,23 @@ void CheckNullPointer::nullPointerByDeRefAndChec()
if (tok1->str() == "break")
break;
if (tok1->varId() == varid && !Token::Match(tok1->previous(), "[?:]"))
if (tok1->varId() == varid)
{
// Don't write warning if the dereferencing is
// guarded by ?:
const Token *tok2 = tok1->previous();
if (tok2 && (tok2->isArithmeticalOp() || tok2->str() == "("))
{
while (tok2 && !Token::Match(tok2, "[;{}?:]"))
{
if (tok2->str() == ")")
tok2 = tok2->link();
tok2 = tok2->previous();
}
}
if (Token::Match(tok2, "[?:]"))
continue;
// unknown : this is set by isPointerDeRef if it is
// uncertain
bool unknown = false;

View File

@ -481,6 +481,31 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
check("void foo(int *p)\n"
"{\n"
" int var1 = x ? *p : 5;\n"
" if (!p)\n"
" ;\n"
"}\n");
TODO_ASSERT_EQUALS("error", "", errout.str());
// Ticket #3125
check("void foo(ABC *p)\n"
"{\n"
" int var1 = p ? (p->a) : 0;\n"
" if (!p)\n"
" ;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void foo(ABC *p)\n"
"{\n"
" int var1 = p ? (1 + p->a) : 0;\n"
" if (!p)\n"
" ;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void foo(P *p)\n"
"{\n"
" while (p)\n"