add support for null pointer checking struct pointers
This commit is contained in:
parent
311651cc66
commit
a99aded1a4
|
@ -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();
|
||||
|
|
|
@ -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()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue