diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 1d5ccbcfc..a8d4a7cb6 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -867,7 +867,7 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() std::list var; parseFunctionCall(*tok2, var, 0); for (std::list::const_iterator it = var.begin(); it != var.end(); ++it) { - if ((*it)->varId() == varid) { + if (Token::Match(*it, "%varid% [,)]", varid)) { nullPointerError(*it, pointerName, linenr, inconclusive); break; } @@ -977,7 +977,7 @@ void CheckNullPointer::nullConstantDereference() // is one of the var items a NULL pointer? for (std::list::const_iterator it = var.begin(); it != var.end(); ++it) { - if ((*it)->str() == "0") { + if (Token::Match(*it, "0 [,)]")) { nullPointerError(*it); } } diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 7ca5fbd55..204d15135 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -1422,6 +1422,12 @@ private: check(code, true); // inconclusive ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: fred - otherwise it is redundant to check if fred is null at line 2\n", errout.str()); } + + check("void f(char *s) {\n" // #3358 + " if (s==0);\n" + " strcpy(a, s?b:c);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } // Test CheckNullPointer::nullConstantDereference @@ -1487,6 +1493,11 @@ private: "}"); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference: s\n", errout.str()); + check("void f() {\n" + " char *s = 0;\n" + " printf(\"%s\", s == 0 ? a : s);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); check("void f() {\n" " printf(\"%u%s\", 0, 0);\n"