From 065af5c444cd3d6b3c344aa92971de10e9e3bed9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 29 Nov 2010 20:30:23 +0100 Subject: [PATCH] Fixed #2251 (False positive: Possible null pointer reference) --- 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 86672e7d5..4f804beec 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), "& (") && tok->strAt(-1) != "&&" && Token::Match(tok->next(), ". %var%")) + if (!Token::simpleMatch(tok->tokAt(-2), "& (") && tok->strAt(-1) != "&" && 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 38fa6db5e..5e421c1f9 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -609,6 +609,14 @@ private: " }\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // ticket #2251: taking the address of member + check("void f() {\n" + " Fred *fred = 0;\n" + " int x = &fred->x;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } void nullpointer7()