diff --git a/lib/checkother.cpp b/lib/checkother.cpp index eb9aa31c7..af730f2b1 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1782,7 +1782,7 @@ private: bailOutVar(checks, tok.varId()); } - if (Token::simpleMatch(&tok, "* 0")) + else if (Token::simpleMatch(&tok, "* 0")) { if (Token::Match(tok.previous(), "[;{}=+-/(,]") || Token::Match(tok.previous(), "return|<<")) @@ -1795,6 +1795,15 @@ private: } } + else if (tok.str() == "delete") + { + const Token *ret = tok.next(); + if (Token::simpleMatch(ret, "[ ]")) + ret = ret->tokAt(2); + if (Token::Match(ret, "%var% ;")) + return ret->next(); + } + return &tok; } diff --git a/test/testother.cpp b/test/testother.cpp index f59d530f8..a50af5678 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -1020,6 +1020,16 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:4]: (error) Null pointer dereference\n", errout.str()); + checkNullPointer("void f()\n" + "{\n" + " char *c = 0;\n" + " {\n" + " delete c;\n" + " }\n" + " c[0] = 0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:7]: (error) Possible null pointer dereference: c\n", errout.str()); + // no false positive.. checkNullPointer("static void foo()\n" "{\n"