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:
parent
e89f6d6ec0
commit
c87037c29d
|
@ -629,6 +629,9 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
|
||||||
// start token = inside the if-body
|
// start token = inside the if-body
|
||||||
const Token *tok1 = tok->next()->link()->tokAt(2);
|
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% )|&&"))
|
if (Token::Match(tok, "if|while ( %var% )|&&"))
|
||||||
{
|
{
|
||||||
// pointer might be null
|
// pointer might be null
|
||||||
|
@ -639,13 +642,15 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
|
||||||
tok1 = tok1 ? tok1->next() : NULL;
|
tok1 = tok1 ? tok1->next() : NULL;
|
||||||
if (!tok1)
|
if (!tok1)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
// indentlevel at the base level is 0
|
||||||
|
indentlevel = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name of the pointer
|
// Name of the pointer
|
||||||
const std::string &pointerName = vartok->str();
|
const std::string &pointerName = vartok->str();
|
||||||
|
|
||||||
// Count { and } for tok2
|
// Count { and } for tok2
|
||||||
unsigned int indentlevel = 1;
|
|
||||||
for (const Token *tok2 = tok1; tok2; tok2 = tok2->next())
|
for (const Token *tok2 = tok1; tok2; tok2 = tok2->next())
|
||||||
{
|
{
|
||||||
if (tok2->str() == "{")
|
if (tok2->str() == "{")
|
||||||
|
@ -655,13 +660,6 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
|
||||||
if (indentlevel == 0)
|
if (indentlevel == 0)
|
||||||
break;
|
break;
|
||||||
--indentlevel;
|
--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"))
|
if (Token::Match(tok2, "goto|return|continue|break|throw|if|switch"))
|
||||||
|
|
|
@ -984,6 +984,23 @@ private:
|
||||||
|
|
||||||
// #2582 - segmentation fault
|
// #2582 - segmentation fault
|
||||||
check("if()");
|
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
|
// Test CheckNullPointer::nullConstantDereference
|
||||||
|
|
Loading…
Reference in New Issue