diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index d62ffe9b2..4ed2b0d38 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -762,6 +762,19 @@ void CheckNullPointer::nullPointerByDeRefAndChec() // Variable id for pointer const unsigned int varid(vartok->varId()); + // bailout for while scope if pointer is assigned inside the loop + if (i->type == Scope::eWhile) { + bool assign = false; + for (const Token *tok2 = i->classStart; tok2 && tok2 != i->classEnd; tok2 = tok2->next()) { + if (Token::Match(tok2, "%varid% =", varid)) { + assign = true; + break; + } + } + if (assign) + continue; + } + // Name of pointer const std::string& varname(vartok->str()); diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index ddb68aa75..f6510b430 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -587,6 +587,19 @@ private: "}"); TODO_ASSERT_EQUALS("error", "", errout.str()); + // while + check("void f(int *p) {\n" + " *p = 0;\n" + " while (p) { p = 0; }\n" + "}"); + ASSERT_EQUALS("", errout.str()); + + check("void f(int *p) {\n" + " *p = 0;\n" + " while (p) { }\n" + "}"); + ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Possible null pointer dereference: p - otherwise it is redundant to check it against null.\n", errout.str()); + // Ticket #3125 check("void foo(ABC *p)\n" "{\n"