null pointers: dereferencing a pointer and then checking if it's null (#49)

This commit is contained in:
Daniel Marjamäki 2009-07-23 14:13:46 +02:00
parent 024778d6eb
commit d0f1d885ed
2 changed files with 62 additions and 1 deletions

View File

@ -1029,7 +1029,7 @@ void CheckOther::nullPointer()
} }
} }
// Dereferencing a pointer and then checking if it's NULL.. // Dereferencing a struct pointer and then checking if it's NULL..
for (const Token *tok1 = _tokenizer->tokens(); tok1; tok1 = tok1->next()) for (const Token *tok1 = _tokenizer->tokens(); tok1; tok1 = tok1->next())
{ {
if (Token::Match(tok1, "[{};] %var% = %var% . %var%")) if (Token::Match(tok1, "[{};] %var% = %var% . %var%"))
@ -1076,6 +1076,37 @@ void CheckOther::nullPointer()
} }
} }
} }
// Dereferencing a pointer and then checking if it's NULL..
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next())
{
if (tok->str() == "if" && Token::Match(tok->previous(), "; if ( ! %var% )"))
{
const unsigned int varid(tok->tokAt(3)->varId());
if (varid == 0)
continue;
for (const Token *tok1 = tok->previous(); tok1; tok1 = tok1->previous())
{
if (tok1->varId() == varid)
{
if (tok1->previous() && tok1->previous()->str() == "*")
{
nullPointerError(tok1);
break;
}
else if (tok1->next() && tok1->next()->str() == "=")
{
break;
}
}
else if (tok1->str() == "{" ||
tok1->str() == "}")
break;
}
}
}
} }

View File

@ -59,6 +59,7 @@ private:
TEST_CASE(nullpointer1); TEST_CASE(nullpointer1);
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(oldStylePointerCast); TEST_CASE(oldStylePointerCast);
} }
@ -410,6 +411,7 @@ private:
Tokenizer tokenizer; Tokenizer tokenizer;
std::istringstream istr(code); std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp"); tokenizer.tokenize(istr, "test.cpp");
tokenizer.simplifyTokenList();
tokenizer.setVarId(); tokenizer.setVarId();
// Clear the error buffer.. // Clear the error buffer..
@ -508,6 +510,34 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
// Dereferencing a pointer and then checking if it is null
void nullpointer4()
{
// errors..
checkNullPointer("void foo(int *p)\n"
"{\n"
" *p = 0;\n"
" if (!p)\n"
" ;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference\n", errout.str());
// no error
checkNullPointer("void foo(int *p)\n"
"{\n"
" if (x)\n"
" p = 0;\n"
" else\n"
" *p = 0;\n"
" if (!p)\n"
" ;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void checkOldStylePointerCast(const char code[]) void checkOldStylePointerCast(const char code[])
{ {
// Tokenize.. // Tokenize..