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()