diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 05d481523..1ea8ab909 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -715,6 +715,21 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() continue; } + // function call, check if pointer is dereferenced + if (Token::Match(tok2, "%var% (")) + { + std::list var; + parseFunctionCall(*tok2, var, 0); + for (std::list::const_iterator it = var.begin(); it != var.end(); ++it) + { + if ((*it)->varId() == varid) + { + nullPointerError(*it, pointerName, linenr); + break; + } + } + } + // calling unknown function (abort/init).. if (Token::simpleMatch(tok2, ") ;") && (Token::Match(tok2->link()->tokAt(-2), "[;{}] %var% (") || diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index f9f1b9009..93ddcd16c 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -985,6 +985,13 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str()); + check("void foo(char *p) {\n" + " if (p) {\n" + " }\n" + " strcpy(p, \"abc\");\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Possible null pointer dereference: p - otherwise it is redundant to check if p is null at line 2\n", errout.str()); + check("void foo(abc *p) {\n" " if (!p) {\n" " }\n"