diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index eb600c197..535e77783 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -533,8 +533,10 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken, return ChildrenToVisit::none; }); - if (!checkScope(closingParenthesis->next(), &varInfo1, notzero, recursiveCount)) + if (!checkScope(closingParenthesis->next(), &varInfo1, notzero, recursiveCount)) { + varInfo->clear(); continue; + } closingParenthesis = closingParenthesis->linkAt(1); if (Token::simpleMatch(closingParenthesis, "} else {")) { if (!checkScope(closingParenthesis->tokAt(2), &varInfo2, notzero, recursiveCount)) diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 01ceb111d..05ccf2650 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -140,6 +140,7 @@ private: // goto TEST_CASE(goto1); TEST_CASE(goto2); + TEST_CASE(goto3); // #11431 // if/else TEST_CASE(ifelse1); @@ -1505,6 +1506,21 @@ private: ASSERT_EQUALS("", errout.str()); } + void goto3() { // #11431 + check("void f() {\n" + " int* p = (int*)malloc(2);\n" + " if (!p) {\n" + " p = (int*)malloc(1);\n" + " if (!p)\n" + " goto err;\n" + " }\n" + " free(p);\n" + "err:\n" + " (void)0;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void ifelse1() { check("int f() {\n" " char *p = NULL;\n"