diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 73e891ebf..d5292306a 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -370,6 +370,11 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken, else if (tok->str() == "goto") { varInfo->clear(); } + + // continue/break + else if (Token::Match(tok, "continue|break ;")) { + varInfo->clear(); + } // throw // TODO: if the execution leave the function then treat it as return diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index d2945e97a..a7dcff4b5 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -56,6 +56,7 @@ private: TEST_CASE(doublefree1); TEST_CASE(doublefree2); + TEST_CASE(doublefree3); // #4914 // exit TEST_CASE(exit1); @@ -327,6 +328,24 @@ private: "}"); ASSERT_EQUALS("", errout.str()); } + + void doublefree3() { // #4914 + check("void foo() {\n" + " bool done = false;\n" + " do {\n" + " char *bar = malloc(10)\n" + " if(condition()) {\n" + " free(bar);\n" + " continue;\n" + " }\n" + " done = true;\n" + " free(bar)\n" + " } while(!done);\n" + " return;" + "}" + ); + ASSERT_EQUALS("", errout.str()); + } void exit1() { check("void f() {\n"