diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index df9bd02cd..90dd548e9 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -1013,6 +1013,8 @@ void CheckNullPointer::nullPointerByCheckAndDeRef() nullPointerError(tok2, pointerName, vartok, inconclusive); else if (unknown) nullPointerError(tok2, pointerName, vartok, true); + if (Token::Match(tok2, "%var% ?")) + break; } } break; diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 34da50ccc..aeef55c8e 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -1679,6 +1679,18 @@ private: " *x = 0;\n" "}"); ASSERT_EQUALS("", errout.str()); + + // return ?: + check("int f(ABC *p) {\n" // FP : return ?: + " if (!p) {}\n" + " return p ? p->x : 0;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + check("int f(ABC *p) {\n" // no fn + " if (!p) {}\n" + " return q ? p->x : 0;\n" + "}"); + ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:2]: (error) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str()); } // Test CheckNullPointer::nullConstantDereference