null pointers: fixed false positives when checking if pointer is null at many locations (#485)

This commit is contained in:
Daniel Marjamäki 2009-07-23 19:59:29 +02:00
parent 06c8ff9d0d
commit 5b81c92a14
2 changed files with 27 additions and 0 deletions

View File

@ -1042,6 +1042,21 @@ void CheckOther::nullPointer()
if (varid1 == 0)
continue;
// Checking if the struct pointer is non-null before the assignment..
{
const Token *tok2 = _tokenizer->tokens();
while (tok2)
{
if (tok2 == tok1)
break;
if (Token::Match(tok2, "if|while ( !| %varid% )", varid1))
break;
tok2 = tok2->next();
}
if (tok2 != tok1)
continue;
}
unsigned int indentlevel2 = 0;
for (const Token *tok2 = tok1->tokAt(3); tok2; tok2 = tok2->next())
{

View File

@ -508,6 +508,18 @@ private:
" ;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
// while..
checkNullPointer("void freeAbc(struct ABC *abc)\n"
"{\n"
" while (abc)\n"
" {\n"
" struct ABC *next = abc->next;\n"
" if (abc) delete abc;\n"
" abc = next;\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
// Dereferencing a pointer and then checking if it is null