From 285d76a413f814cfadc6b32966f1382e6dc076f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 31 Oct 2010 19:48:58 +0100 Subject: [PATCH] Null pointers: Fixed false positive for 'x && x->y' --- lib/checknullpointer.cpp | 2 +- test/testnullpointer.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index 08b94fcf7..cc5d9cda2 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -115,7 +115,7 @@ bool CheckNullPointer::isPointerDeRef(const Token *tok, bool &unknown) if (Token::Match(tok->tokAt(-3), "!!sizeof [;{}=+-/(,] * %var%")) return true; - if (!Token::simpleMatch(tok->tokAt(-2), "& (") && Token::Match(tok->next(), ". %var%")) + if (!Token::simpleMatch(tok->tokAt(-2), "& (") && tok->strAt(-1) != "&&" && Token::Match(tok->next(), ". %var%")) return true; if (Token::Match(tok->previous(), "[;{}=+-/(,] %var% [")) diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 76d409eb7..69357d396 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -362,6 +362,14 @@ private: " p = p->next();\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void f(Document *doc) {\n" + " int x = doc && doc->x;\n" + " if (!doc) {\n" + " return;\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void nullpointer5()