Fixed #485 (detect when code is checking for null after dereferencing)
This commit is contained in:
parent
f0afdf3582
commit
b7ba49114c
|
@ -1024,6 +1024,32 @@ void CheckOther::nullPointer()
|
|||
tok2 = tok2->next();
|
||||
}
|
||||
}
|
||||
|
||||
// Dereferencing a pointer and then checking if it's NULL..
|
||||
for (const Token *tok1 = _tokenizer->tokens(); tok1; tok1 = tok1->next())
|
||||
{
|
||||
if (Token::Match(tok1, "%var% . %var%"))
|
||||
{
|
||||
const unsigned int varid1(tok1->varId());
|
||||
unsigned int indentlevel2 = 0;
|
||||
for (const Token *tok2 = tok1->tokAt(3); tok2; tok2 = tok2->next())
|
||||
{
|
||||
if (tok2->str() == "{")
|
||||
++indentlevel2;
|
||||
else if (tok2->str() == "}")
|
||||
{
|
||||
if (indentlevel2 == 0)
|
||||
break;
|
||||
--indentlevel2;
|
||||
}
|
||||
else if (tok2->str() == "if")
|
||||
{
|
||||
if (Token::Match(tok2, "if ( !| %varid% )", varid1))
|
||||
nullPointerError(tok1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -58,6 +58,8 @@ private:
|
|||
|
||||
TEST_CASE(nullpointer1);
|
||||
TEST_CASE(nullpointer2);
|
||||
TEST_CASE(nullpointer3);
|
||||
TEST_CASE(nullpointer4);
|
||||
|
||||
TEST_CASE(oldStylePointerCast);
|
||||
}
|
||||
|
@ -444,6 +446,28 @@ private:
|
|||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void nullpointer3()
|
||||
{
|
||||
checkNullPointer("void foo(struct ABC *abc)\n"
|
||||
"{\n"
|
||||
" int *a = abc->a;\n"
|
||||
" if (!abc)\n"
|
||||
" ;\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference\n", errout.str());
|
||||
}
|
||||
|
||||
void nullpointer4()
|
||||
{
|
||||
checkNullPointer("void foo(struct ABC *abc)\n"
|
||||
"{\n"
|
||||
" int *a = abc->a;\n"
|
||||
" if (abc)\n"
|
||||
" ;\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference\n", errout.str());
|
||||
}
|
||||
|
||||
void checkOldStylePointerCast(const char code[])
|
||||
{
|
||||
// Tokenize..
|
||||
|
|
Loading…
Reference in New Issue