From f127d56719c436a07ac79324b10235e257f86e96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 25 Dec 2012 14:23:50 +0100 Subject: [PATCH] Null pointer: Fixed FP when there are 'return p?p->x:0;' --- lib/checknullpointer.cpp | 2 ++ test/testnullpointer.cpp | 12 ++++++++++++ 2 files changed, 14 insertions(+) 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