Fixed #1219 (improve check: null pointer not detected 'if (p) return; *p = 0;')
This commit is contained in:
parent
79ef02812d
commit
2848abbf36
|
@ -526,6 +526,9 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
|
||||||
vartok = tok->tokAt(4);
|
vartok = tok->tokAt(4);
|
||||||
else if (Token::Match(tok, "if ( %var% == NULL|0 ) {"))
|
else if (Token::Match(tok, "if ( %var% == NULL|0 ) {"))
|
||||||
vartok = tok->tokAt(2);
|
vartok = tok->tokAt(2);
|
||||||
|
else if (Token::Match(tok, "if|while ( %var% ) {") &&
|
||||||
|
!Token::simpleMatch(tok->tokAt(4)->link(), "} else"))
|
||||||
|
vartok = tok->tokAt(2);
|
||||||
else
|
else
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -541,12 +544,24 @@ void CheckNullPointer::nullPointerByCheckAndDeRef()
|
||||||
// if this is true then it is known that the pointer is null
|
// if this is true then it is known that the pointer is null
|
||||||
bool null = true;
|
bool null = true;
|
||||||
|
|
||||||
|
// start token = inside the if-body
|
||||||
|
const Token *tok1 = tok->next()->link()->tokAt(2);
|
||||||
|
|
||||||
|
if (Token::Match(tok, "if|while ( %var% )"))
|
||||||
|
{
|
||||||
|
// pointer might be null
|
||||||
|
null = false;
|
||||||
|
|
||||||
|
// start token = first token after the if/while body
|
||||||
|
tok1 = tok1->previous()->link()->next();
|
||||||
|
}
|
||||||
|
|
||||||
// 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;
|
unsigned int indentlevel = 1;
|
||||||
for (const Token *tok2 = tok->next()->link()->tokAt(2); tok2; tok2 = tok2->next())
|
for (const Token *tok2 = tok1; tok2; tok2 = tok2->next())
|
||||||
{
|
{
|
||||||
if (tok2->str() == "{")
|
if (tok2->str() == "{")
|
||||||
++indentlevel;
|
++indentlevel;
|
||||||
|
|
|
@ -818,6 +818,15 @@ private:
|
||||||
" }\n"
|
" }\n"
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
|
||||||
|
// ticket #1219
|
||||||
|
check("void foo(char *p) {\n"
|
||||||
|
" if (p) {\n"
|
||||||
|
" return;\n"
|
||||||
|
" }\n"
|
||||||
|
" *p = 0;\n"
|
||||||
|
"}\n");
|
||||||
|
ASSERT_EQUALS("[test.cpp:5]: (error) Possible null pointer dereference: p\n", errout.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test CheckNullPointer::nullConstantDereference
|
// Test CheckNullPointer::nullConstantDereference
|
||||||
|
|
Loading…
Reference in New Issue