diff --git a/lib/checkother.cpp b/lib/checkother.cpp index eab8061d1..7bfb846a5 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1198,6 +1198,8 @@ private: dereference(foundError, checks, &tok); else if (Token::Match(tok.next(), ". %var%")) dereference(foundError, checks, &tok); + else if (Token::Match(tok.previous(), "[;{}] %var% [ %num% ] =")) + dereference(foundError, checks, &tok); else bailOutVar(checks, tok.varId()); } diff --git a/test/testother.cpp b/test/testother.cpp index 5e4288124..cbd25a580 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -615,6 +615,8 @@ private: settings._checkCodingStyle = true; CheckOther checkOther(&tokenizer, &settings, this); checkOther.nullPointer(); + + tokenizer.simplifyTokenList(); checkOther.executionPaths(); } @@ -905,6 +907,14 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:8]: (error) Possible null pointer dereference: p\n", errout.str()); + checkNullPointer("static void foo()\n" + "{\n" + " int *p = 0;\n" + " int *q = p;\n" + " q[0] = 0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:5]: (error) Possible null pointer dereference: q\n", errout.str()); + // no false positive.. checkNullPointer("static void foo()\n" "{\n"