diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 05c4e974e..1cd62a57a 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2501,7 +2501,7 @@ void CheckOther::checkDoubleFree() // If a variable is passed to a function, remove it from the set of previously freed variables - else if (Token::Match(tok, "%var% (") && !Token::Match(tok, "printf|sprintf|snprintf|fprintf|if|while")) { + else if (Token::Match(tok, "%var% (") && !Token::Match(tok, "printf|sprintf|snprintf|fprintf")) { // If this is a new function definition, clear all variables if (Token::simpleMatch(tok->next()->link(), ") {")) { @@ -2533,7 +2533,7 @@ void CheckOther::checkDoubleFree() // Any control statements in-between delete, free() or closedir() statements // makes it unclear whether any subsequent statements would be redundant. - if (Token::Match(tok, "else|break|continue|goto|return|throw")) { + if (Token::Match(tok, "if|else|for|while|break|continue|goto|return|throw|switch")) { freedVariables.clear(); closeDirVariables.clear(); } diff --git a/test/testother.cpp b/test/testother.cpp index 7252c73e4..5f3e0e026 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -4614,6 +4614,22 @@ private: "}" ); ASSERT_EQUALS("", errout.str()); + + check( + "int foo()\n" + "{\n" + " int* a = new int;\n" + " bool doDelete = true;\n" + " if (a != 0)\n" + " {\n" + " doDelete = false;\n" + " delete a;\n" + " }\n" + " if(doDelete)\n" + " delete a;\n" + " return 0;\n" + "}" + ); } void coutCerrMisusage() {