Fixed #2674 (False positive: possible null pointer dereference (check if pointer is null in one function, dereference it in another function))

This commit is contained in:
Daniel Marjamäki 2011-03-27 11:04:58 +02:00
parent e89f6d6ec0
commit c87037c29d
2 changed files with 23 additions and 8 deletions

View File

@ -629,6 +629,9 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
// start token = inside the if-body
const Token *tok1 = tok->next()->link()->tokAt(2);
// indentlevel inside the if-body is 1
unsigned int indentlevel = 1;
if (Token::Match(tok, "if|while ( %var% )|&&"))
{
// pointer might be null
@ -639,13 +642,15 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
tok1 = tok1 ? tok1->next() : NULL;
if (!tok1)
continue;
// indentlevel at the base level is 0
indentlevel = 0;
}
// Name of the pointer
const std::string &pointerName = vartok->str();
// Count { and } for tok2
unsigned int indentlevel = 1;
for (const Token *tok2 = tok1; tok2; tok2 = tok2->next())
{
if (tok2->str() == "{")
@ -655,13 +660,6 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
if (indentlevel == 0)
break;
--indentlevel;
if (null && indentlevel == 0)
{
// skip all "else" blocks because they are not executed in this execution path
while (Token::simpleMatch(tok2, "} else {"))
tok2 = tok2->tokAt(2)->link();
null = false;
}
}
if (Token::Match(tok2, "goto|return|continue|break|throw|if|switch"))

View File

@ -984,6 +984,23 @@ private:
// #2582 - segmentation fault
check("if()");
// #2674 - different functions
check("class Fred {\n"
"public:\n"
" Wilma *wilma;\n"
" void a();\n"
" void b();\n"
"};\n"
"\n"
"void Fred::a() {\n"
" if ( wilma ) { }\n"
"}\n"
"\n"
"void Fred::b() {\n"
" wilma->Reload();\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
// Test CheckNullPointer::nullConstantDereference