Fixed #839 (False positive: possible null pointer dereference after new)

This commit is contained in:
Daniel 2009-10-20 20:57:38 +02:00
parent 4f7df21851
commit e5f13b4de2
2 changed files with 17 additions and 4 deletions

View File

@ -1111,14 +1111,14 @@ void CheckOther::nullPointerConditionalAssignment()
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{ {
// 1. Initialize.. // 1. Initialize..
if (!tok->Match(tok, "; %var% = 0 ;")) if (!Token::Match(tok, "; %var% = 0 ;"))
continue; continue;
const unsigned int varid(tok->next()->varId()); const unsigned int varid(tok->next()->varId());
if (!varid) if (!varid)
continue; continue;
for (const Token *tok2 = tok; tok2; tok2 = tok2->next()) for (const Token *tok2 = tok->tokAt(4); tok2; tok2 = tok2->next())
{ {
if (tok2->str() == "{" || tok2->str() == "}") if (tok2->str() == "{" || tok2->str() == "}")
break; break;
@ -1141,8 +1141,12 @@ void CheckOther::nullPointerConditionalAssignment()
if (Token::Match(tok2, "else !!if")) if (Token::Match(tok2, "else !!if"))
break; break;
if (Token::Match(tok2, "%varid%", varid))
{
if (Token::Match(tok2, "%varid% . %var% (", varid)) if (Token::Match(tok2, "%varid% . %var% (", varid))
nullPointerError(tok2, tok->next()->str()); nullPointerError(tok2, tok->next()->str());
break;
}
} }
} }
} }

View File

@ -899,6 +899,15 @@ private:
" p->abcd();\n" " p->abcd();\n"
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:8]: (error) Possible null pointer dereference: p\n", errout.str()); ASSERT_EQUALS("[test.cpp:8]: (error) Possible null pointer dereference: p\n", errout.str());
// no false positive..
checkNullPointer("static void foo()\n"
"{\n"
" Foo *p = 0;\n"
" p = new Foo;\n"
" p->abcd();\n"
"}\n");
ASSERT_EQUALS("", errout.str());
} }
void checkOldStylePointerCast(const char code[]) void checkOldStylePointerCast(const char code[])