From fc26de89a93b986cf3ce03dc49ec1edbae1cd4f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 20 Oct 2013 13:37:36 +0200 Subject: [PATCH] Fixed #5082 (False positive: (error) Possible null pointer dereference: p2) --- lib/checknullpointer.cpp | 8 ++++---- test/testnullpointer.cpp | 10 ++++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) 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"