diff --git a/src/checkother.cpp b/src/checkother.cpp index 2f034a5ad..7fd276892 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -1159,7 +1159,7 @@ void CheckOther::nullPointer() else if (Token::Match(tok2, "if ( !| %varid% )", varid1)) { - nullPointerError(tok1, varname); + nullPointerError(tok1, varname, tok2->linenr()); break; } } @@ -1344,6 +1344,13 @@ void CheckOther::nullPointerError(const Token *tok, const std::string &varname) reportError(tok, Severity::error, "nullPointer", "Possible null pointer dereference: " + varname); } +void CheckOther::nullPointerError(const Token *tok, const std::string &varname, const int line) +{ + std::ostringstream ostr; + ostr << line; + reportError(tok, Severity::error, "nullPointer", "Possible null pointer dereference: " + varname + " - otherwise it is redundant to check if " + varname + " is null at line " + ostr.str()); +} + void CheckOther::zerodivError(const Token *tok) { reportError(tok, Severity::error, "zerodiv", "Division by zero"); diff --git a/src/checkother.h b/src/checkother.h index c8e782d2d..e93b24d18 100644 --- a/src/checkother.h +++ b/src/checkother.h @@ -143,6 +143,7 @@ public: void conditionAlwaysTrueFalse(const Token *tok, const std::string &truefalse); void strPlusChar(const Token *tok); void nullPointerError(const Token *tok, const std::string &varname); + void nullPointerError(const Token *tok, const std::string &varname, const int line); void zerodivError(const Token *tok); void postIncrementError(const Token *tok, const std::string &var_name, const bool isIncrement); diff --git a/test/testother.cpp b/test/testother.cpp index d1e2fd07a..1b5c96747 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -492,7 +492,7 @@ private: " if (!abc)\n" " ;\n" "}\n"); - ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: abc\n", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: abc - otherwise it is redundant to check if abc is null at line 4\n", errout.str()); // ok dereferencing in a condition checkNullPointer("void foo(struct ABC *abc)\n"