diff --git a/lib/checkother.cpp b/lib/checkother.cpp index abd8171e9..88cf544af 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1275,6 +1275,19 @@ private: bailOutVar(checks, tok.varId()); } + if (Token::simpleMatch(&tok, "* 0")) + { + if (Token::Match(tok.previous(), "[;{}=]")) + { + CheckOther *checkOther = dynamic_cast(owner); + if (checkOther) + { + checkOther->nullPointerError(&tok); + foundError = true; + } + } + } + return &tok; } @@ -1867,6 +1880,11 @@ void CheckOther::strPlusChar(const Token *tok) reportError(tok, Severity::error, "strPlusChar", "Unusual pointer arithmetic"); } +void CheckOther::nullPointerError(const Token *tok) +{ + reportError(tok, Severity::error, "nullPointer", "Null pointer dereference"); +} + void CheckOther::nullPointerError(const Token *tok, const std::string &varname) { reportError(tok, Severity::error, "nullPointer", "Possible null pointer dereference: " + varname); diff --git a/lib/checkother.h b/lib/checkother.h index d7ba3e92c..3228fc42f 100644 --- a/lib/checkother.h +++ b/lib/checkother.h @@ -147,6 +147,7 @@ public: void variableScopeError(const Token *tok, const std::string &varname); void conditionAlwaysTrueFalse(const Token *tok, const std::string &truefalse); void strPlusChar(const Token *tok); + void nullPointerError(const Token *tok); // variable name unknown / doesn't exist void nullPointerError(const Token *tok, const std::string &varname); void nullPointerError(const Token *tok, const std::string &varname, const int line); void uninitdataError(const Token *tok, const std::string &varname); diff --git a/test/testother.cpp b/test/testother.cpp index 67382544a..d57f13e96 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -915,6 +915,13 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:5]: (error) Possible null pointer dereference: q\n", errout.str()); + checkNullPointer("static void foo()\n" + "{\n" + " int *p = 0;\n" + " int &r = *p;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference\n", errout.str()); + // no false positive.. checkNullPointer("static void foo()\n" "{\n"