From 0b2ad2164f516862d3900e09b9d575dfbd692bbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 22 Feb 2010 21:30:21 +0100 Subject: [PATCH] Fixed #1442 (false positive: getting offset instead of dereferencing a null pointer) --- lib/checkother.cpp | 2 +- test/testother.cpp | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 059595c54..2f6ee86a2 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1521,7 +1521,7 @@ private: dereference(foundError, checks, &tok); else if (Token::Match(tok.tokAt(-2), "return * %var%")) dereference(foundError, checks, &tok); - else if (Token::Match(tok.next(), ". %var%")) + else if (!Token::simpleMatch(tok.tokAt(-2), "& (") && Token::Match(tok.next(), ". %var%")) dereference(foundError, checks, &tok); else if (Token::Match(tok.previous(), "[;{}=+-/(,] %var% [ %any% ]")) dereference(foundError, checks, &tok); diff --git a/test/testother.cpp b/test/testother.cpp index 7ee950c3e..99930949f 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -441,7 +441,7 @@ private: strPlusChar("void foo()\n" "{\n" " int i = 1;\n" - " const char* psz = \"Bla\";\n" + " const char* psz = \"Bla\";\n" " const std::string str = i + psz;\n" "}\n"); ASSERT_EQUALS("", errout.str()); @@ -1015,6 +1015,13 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + checkNullPointer("void get_offset(long &offset)\n" + "{\n" + " mystruct * temp; temp = 0;\n" + " offset = (long)(&(temp->z));\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + // function pointer.. checkNullPointer("void foo()\n" "{\n"