From e6713e9774196816173a2539d42f55372da63df9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 1 Aug 2009 11:30:37 +0200 Subject: [PATCH] null pointers: fixed a false positive --- src/checkother.cpp | 3 +++ test/testother.cpp | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/checkother.cpp b/src/checkother.cpp index 930aecb0b..4ade42d75 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -1042,6 +1042,9 @@ void CheckOther::nullPointer() if (varid == 0) continue; + if (Token::Match(tok2->tokAt(-2), "%varid% ?", varid)) + continue; + // Check usage of dereferenced variable in the loop.. unsigned int indentlevel3 = 0; for (const Token *tok3 = tok1->next()->link(); tok3; tok3 = tok3->next()) diff --git a/test/testother.cpp b/test/testother.cpp index 1a7eeb068..5c528080d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -446,6 +446,16 @@ private: " }\n" "}\n"); ASSERT_EQUALS("[test.cpp:3]: (error) Possible null pointer dereference\n", errout.str()); + + checkNullPointer("void foo()\n" + "{\n" + " for (const Token *tok = tokens; tok; tok = tok ? tok->next() : NULL)\n" + " {\n" + " while (tok && tok->str() != \";\")\n" + " tok = tok->next();\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void nullpointer2()