Fix #802 (possible null pointer dereference reported for reference)

http://sourceforge.net/apps/trac/cppcheck/ticket/802
This commit is contained in:
Reijo Tomperi 2009-10-08 11:55:37 +03:00
parent f83554e534
commit ad5d87ee14
2 changed files with 32 additions and 5 deletions

View File

@ -878,7 +878,7 @@ void CheckOther::nullPointer()
if (tok2->next()->str() == "." || Token::Match(tok2->next(), "= %varid% .", varid)) if (tok2->next()->str() == "." || Token::Match(tok2->next(), "= %varid% .", varid))
{ {
// Is this variable a pointer? // Is this variable a pointer?
const Token *tok3 = Token::findmatch(_tokenizer->tokens(), "%type% * %varid% [;)]", varid); const Token *tok3 = Token::findmatch(_tokenizer->tokens(), "%type% * %varid% [;)=]", varid);
if (!tok3) if (!tok3)
break; break;
@ -956,7 +956,11 @@ void CheckOther::nullPointer()
{ {
if (indentlevel4 <= 1) if (indentlevel4 <= 1)
{ {
nullPointerError(tok1, varname); // Is this variable a pointer?
const Token *tempTok = Token::findmatch(_tokenizer->tokens(), "%type% * %varid% [;)=]", varid);
if (tempTok)
nullPointerError(tok1, varname);
break; break;
} }
--indentlevel4; --indentlevel4;
@ -1037,7 +1041,10 @@ void CheckOther::nullPointer()
else if (Token::Match(tok2, "if ( !| %varid% )", varid1)) else if (Token::Match(tok2, "if ( !| %varid% )", varid1))
{ {
nullPointerError(tok1, varname, tok2->linenr()); // Is this variable a pointer?
const Token *tempTok = Token::findmatch(_tokenizer->tokens(), "%type% * %varid% [;)=]", varid1);
if (tempTok)
nullPointerError(tok1, varname, tok2->linenr());
break; break;
} }
} }

View File

@ -65,6 +65,7 @@ private:
TEST_CASE(nullpointer2); TEST_CASE(nullpointer2);
TEST_CASE(nullpointer3); // dereferencing struct and then checking if it's null TEST_CASE(nullpointer3); // dereferencing struct and then checking if it's null
TEST_CASE(nullpointer4); TEST_CASE(nullpointer4);
TEST_CASE(nullpointer5); // References should not be checked
TEST_CASE(oldStylePointerCast); TEST_CASE(oldStylePointerCast);
@ -632,6 +633,16 @@ private:
"}\n"); "}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: tok\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: tok\n", errout.str());
checkNullPointer("void foo(Token &tok)\n"
"{\n"
" for (int i = 0; i < tok.size(); i++ )\n"
" {\n"
" while (!tok)\n"
" char c = tok.read();\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout.str());
checkNullPointer("void foo()\n" checkNullPointer("void foo()\n"
"{\n" "{\n"
" for (const Token *tok = tokens; tok; tok = tok->next())\n" " for (const Token *tok = tokens; tok; tok = tok->next())\n"
@ -862,8 +873,17 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void nullpointer5()
{
// errors..
checkNullPointer("void foo(A &a)\n"
"{\n"
" char c = a.c();\n"
" if (!a)\n"
" return;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void checkOldStylePointerCast(const char code[]) void checkOldStylePointerCast(const char code[])
{ {