Fix #9966: FP memleak with multiple assignments in if-statement (#4138)

Similar to how we do for assignments outside if-statements, bail out
since we do not track multiple variables.
This commit is contained in:
Rikard Falkeborn 2022-05-27 07:57:43 +02:00 committed by GitHub
parent 19dd59eae6
commit 068e5bad27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -441,7 +441,8 @@ void CheckLeakAutoVar::checkScope(const Token * const startToken,
if (!isLocalVarNoAutoDealloc(innerTok, mTokenizer->isCPP()))
continue;
if (Token::Match(innerTok, "%var% =") && innerTok->astParent() == innerTok->next()) {
// Check assignments in the if-statement. Skip multiple assignments since we don't track those
if (Token::Match(innerTok, "%var% =") && innerTok->astParent() == innerTok->next() && !innerTok->next()->astParent()->isAssignmentOp()) {
// allocation?
// right ast part (after `=` operator)
const Token* tokRightAstOperand = innerTok->next()->astOperand2();

View File

@ -164,6 +164,7 @@ private:
TEST_CASE(ifelse22); // #10187
TEST_CASE(ifelse23); // #5473
TEST_CASE(ifelse24); // #1733
TEST_CASE(ifelse25); // #9966
// switch
TEST_CASE(switch1);
@ -1827,6 +1828,17 @@ private:
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Memory leak: temp\n", "", errout.str());
}
void ifelse25() { // #9966
check("void f() {\n"
" void *p, *p2;\n"
" if((p2 = p = malloc(10)) == NULL)\n"
" return;\n"
" (void)p;\n"
" free(p2);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void switch1() {
check("void f() {\n"
" char *p = 0;\n"