add support for null pointer checking struct pointers

This commit is contained in:
Robert Reif 2011-05-27 23:30:19 -04:00
parent 311651cc66
commit a99aded1a4
2 changed files with 38 additions and 1 deletions

View File

@ -878,13 +878,16 @@ private:
/** parse tokens */
const Token *parse(const Token &tok, std::list<ExecutionPath *> &checks) const
{
if (Token::Match(tok.previous(), "[;{}] const| %type% * %var% ;"))
if (Token::Match(tok.previous(), "[;{}] const| struct| %type% * %var% ;"))
{
const Token * vartok = tok.tokAt(2);
if (tok.str() == "const")
vartok = vartok->next();
if (tok.str() == "struct")
vartok = vartok->next();
if (vartok->varId() != 0)
checks.push_back(new Nullpointer(owner, vartok->varId(), vartok->str()));
return vartok->next();

View File

@ -44,6 +44,7 @@ private:
TEST_CASE(nullpointer7);
TEST_CASE(nullpointer8);
TEST_CASE(nullpointer9);
TEST_CASE(nullpointer10);
TEST_CASE(pointerCheckAndDeRef); // check if pointer is null and then dereference it
TEST_CASE(nullConstantDereference); // Dereference NULL constant
TEST_CASE(gcc_statement_expression); // Don't crash
@ -871,6 +872,39 @@ private:
ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference\n", errout.str());
}
void nullpointer10()
{
check("void foo()\n"
"{\n"
" struct my_type* p = 0;\n"
" p->x = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p\n", errout.str());
check("void foo()\n"
"{\n"
" struct my_type* p;\n"
" p = 0; \n"
" p->x = 0;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5]: (error) Possible null pointer dereference: p\n", errout.str());
check("int foo()\n"
"{\n"
" struct my_type* p = 0;\n"
" return p->x;\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p\n", "", errout.str());
check("int foo()\n"
"{\n"
" struct my_type* p;\n"
" p = 0; \n"
" return p->x;\n"
"}\n");
TODO_ASSERT_EQUALS("[test.cpp:5]: (error) Possible null pointer dereference: p\n", "", errout.str());
}
// Check if pointer is null and the dereference it
void pointerCheckAndDeRef()
{