Skip to content

Commit b7693cc

Browse files
Fix #11431 FP resourceLeak with goto (#4645)
1 parent 29e0133 commit b7693cc

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

lib/checkleakautovar.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,10 @@ bool CheckLeakAutoVar::checkScope(const Token * const startToken,
533533
return ChildrenToVisit::none;
534534
});
535535

536-
if (!checkScope(closingParenthesis->next(), &varInfo1, notzero, recursiveCount))
536+
if (!checkScope(closingParenthesis->next(), &varInfo1, notzero, recursiveCount)) {
537+
varInfo->clear();
537538
continue;
539+
}
538540
closingParenthesis = closingParenthesis->linkAt(1);
539541
if (Token::simpleMatch(closingParenthesis, "} else {")) {
540542
if (!checkScope(closingParenthesis->tokAt(2), &varInfo2, notzero, recursiveCount))

test/testleakautovar.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ class TestLeakAutoVar : public TestFixture {
140140
// goto
141141
TEST_CASE(goto1);
142142
TEST_CASE(goto2);
143+
TEST_CASE(goto3); // #11431
143144

144145
// if/else
145146
TEST_CASE(ifelse1);
@@ -1505,6 +1506,21 @@ class TestLeakAutoVar : public TestFixture {
15051506
ASSERT_EQUALS("", errout.str());
15061507
}
15071508

1509+
void goto3() { // #11431
1510+
check("void f() {\n"
1511+
" int* p = (int*)malloc(2);\n"
1512+
" if (!p) {\n"
1513+
" p = (int*)malloc(1);\n"
1514+
" if (!p)\n"
1515+
" goto err;\n"
1516+
" }\n"
1517+
" free(p);\n"
1518+
"err:\n"
1519+
" (void)0;\n"
1520+
"}\n");
1521+
ASSERT_EQUALS("", errout.str());
1522+
}
1523+
15081524
void ifelse1() {
15091525
check("int f() {\n"
15101526
" char *p = NULL;\n"

0 commit comments

Comments
 (0)