Fix #11431 FP resourceLeak with goto (#4645)

This commit is contained in:
chrchr-github 2022-12-14 22:47:14 +01:00 committed by GitHub
parent 29e0133cb5
commit b7693ccc7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -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))

View File

@ -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"