diff --git a/lib/checknullpointer.cpp b/lib/checknullpointer.cpp index f3525b1ca..aae184842 100644 --- a/lib/checknullpointer.cpp +++ b/lib/checknullpointer.cpp @@ -1401,12 +1401,12 @@ private: bool null; /** variable is set to null */ - static void setnull(std::list &checks, const unsigned int varid) { + static void setnull(std::list &checks, const unsigned int varid, bool null) { std::list::iterator it; for (it = checks.begin(); it != checks.end(); ++it) { Nullpointer *c = dynamic_cast(*it); if (c && c->varId == varid) - c->null = true; + c->null = null; } } @@ -1483,8 +1483,8 @@ private: dereference(checks, &tok); else if (unknown && owner->inconclusiveFlag()) dereference(checks, &tok); - if (Token::Match(tok.previous(), "[;{}=] %var% = 0 ;")) - setnull(checks, tok.varId()); + if (Token::Match(tok.previous(), "[;{}=] %var% =")) + setnull(checks, tok.varId(), Token::simpleMatch(tok.tokAt(2), "0 ;")); else if (!deref && (!tok.previous()->isOp() || tok.previous()->str() == "&") && (!tok.next()->isConstOp() || tok.next()->str() == ">>")) diff --git a/test/testnullpointer.cpp b/test/testnullpointer.cpp index 312deb0d5..41aee3f58 100644 --- a/test/testnullpointer.cpp +++ b/test/testnullpointer.cpp @@ -55,6 +55,7 @@ private: TEST_CASE(nullpointer20); // #3807 (fp: return p ? (p->x() || p->y()) : z) TEST_CASE(nullpointer21); // #4038 (fp: if (x) p=q; else return;) TEST_CASE(nullpointer23); // #4665 (false positive) + TEST_CASE(nullpointer24); // #5082 fp: chained assignment TEST_CASE(nullpointer_cast); // #4692 TEST_CASE(nullpointer_castToVoid); // #3771 TEST_CASE(pointerCheckAndDeRef); // check if pointer is null and then dereference it @@ -1241,6 +1242,15 @@ private: TODO_ASSERT_EQUALS("","[test.cpp:4]: (error) Possible null pointer dereference: c\n", errout.str()); } + void nullpointer24() { // #5083 - fp: chained assignment + check("void f(){\n" + " char *c = NULL;\n" + " x = c = new char[10];\n" + " *c = 0;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void nullpointer_cast() { // #4692 check("char *nasm_skip_spaces(const char *p) {\n" " if (p)\n"