diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 1ea8ab909..325503caa 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -735,7 +735,15 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() (Token::Match(tok2->link()->tokAt(-2), "[;{}] %var% (") || Token::Match(tok2->link()->tokAt(-5), "[;{}] ( * %var% ) ("))) { - break; + // noreturn function? + if (tok2->strAt(2) == "}") + break; + + // init function (global variables) + const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase(); + const Variable *var = symbolDatabase->getVariableFromVarId(varid); + if (!var || !(var->isLocal() || var->isArgument())) + break; } if (tok2->varId() == varid) diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 93ddcd16c..f2c3174ac 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -992,6 +992,14 @@ private: "}\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(char *p) {\n" + " if (p) {\n" + " }\n" + " bar();\n" + " strcpy(p, \"abc\");\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:5]: (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"