null pointer dereferencing: check that its a pointer that is dereferenced to avoid false positives when using classes that behave almost like pointers (#295)

This commit is contained in:
Daniel Marjamäki 2009-05-10 08:43:16 +02:00
parent daea0547fa
commit 1ae24066fe
2 changed files with 26 additions and 1 deletions

View File

@ -964,7 +964,19 @@ void CheckOther::nullPointer()
if (tok2->varId() == varid)
{
if (tok2->next()->str() == "." || Token::Match(tok2->next(), "= %varid% .", varid))
{
// Is this variable a pointer?
const Token *tok3 = Token::findmatch(_tokenizer->tokens(), "%type% * %varid% [;)]", varid);
if (!tok3)
break;
if (!tok3->previous() ||
Token::Match(tok3->previous(), "[({};]") ||
tok3->previous()->isName())
{
nullPointerError(tok2);
}
}
break;
}

View File

@ -59,6 +59,7 @@ private:
TEST_CASE(varScope2);
TEST_CASE(nullpointer1);
TEST_CASE(nullpointer2);
}
void check(const char code[])
@ -411,6 +412,18 @@ private:
"}\n");
ASSERT_EQUALS(std::string("[test.cpp:4]: (error) Possible null pointer dereference\n"), errout.str());
}
void nullpointer2()
{
// Null pointer dereference can only happen with pointers
checkNullPointer("void foo()\n"
"{\n"
" Fred fred;\n"
" while (fred);\n"
" fred.hello();\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
}
};
REGISTER_TEST(TestOther)