diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index f7eb0f33c1b..97678273155 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -609,7 +609,7 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken, // unknown control.. (TODO: handle loops) else if ((Token::Match(tok, "%type% (") && Token::simpleMatch(tok->linkAt(1), ") {")) || Token::simpleMatch(tok, "do {")) { varInfo.clear(); - break; + return false; } // return diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 7df909f8bf5..9d61b7cd7d1 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -178,6 +178,7 @@ class TestLeakAutoVar : public TestFixture { // loops TEST_CASE(loop1); + TEST_CASE(loop2); // mismatching allocation/deallocation TEST_CASE(mismatchAllocDealloc); @@ -2070,6 +2071,17 @@ class TestLeakAutoVar : public TestFixture { ASSERT_EQUALS("", errout.str()); } + void loop2() { + check("void f() {\n" // #11786 + " int* p = (int*)malloc(sizeof(int));\n" + " if (1) {\n" + " while (0) {}\n" + " free(p);\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void mismatchAllocDealloc() { check("void f() {\n" " FILE*f=fopen(fname,a);\n"