diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 193711ea7..8dca3e7c0 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -878,13 +878,16 @@ private: /** parse tokens */ const Token *parse(const Token &tok, std::list &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(); diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index af6091457..259fe8314 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -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() {