Skip to content

Commit 88bf11a

Browse files
Fix #10857 FN: leakNoVarFunctionCall (#4236)
* Fix #10857 FN: leakNoVarFunctionCall * Fix TODO
1 parent 242afc3 commit 88bf11a

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

lib/checkmemoryleak.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,8 +1016,10 @@ void CheckMemoryLeakNoVar::checkForUnreleasedInputArgument(const Scope *scope)
10161016
for (const Token* arg : args) {
10171017
if (arg->isOp())
10181018
continue;
1019-
while (arg->astOperand1())
1020-
arg = arg->astOperand1();
1019+
if (!(mTokenizer->isCPP() && Token::simpleMatch(arg, "new"))) {
1020+
while (arg->astOperand1())
1021+
arg = arg->astOperand1();
1022+
}
10211023
if (getAllocationType(arg, 0) == No)
10221024
continue;
10231025
if (isReopenStandardStream(arg))
@@ -1068,8 +1070,9 @@ void CheckMemoryLeakNoVar::checkForUnusedReturnValue(const Scope *scope)
10681070
if (closingBrace->str() == "}" && Token::Match(closingBrace->link()->tokAt(-1), "%name%") && (!isNew && precedes(tok, closingBrace->link())))
10691071
continue;
10701072
returnValueNotUsedError(tok, tok->str());
1071-
} else if (Token::Match(parent, "%comp%|!")) {
1072-
returnValueNotUsedError(tok, tok->str());
1073+
} else if (Token::Match(parent, "%comp%|!|,")) {
1074+
if (!(parent->astParent() && parent->str() == ","))
1075+
returnValueNotUsedError(tok, tok->str());
10731076
}
10741077
}
10751078
}

test/testmemleak.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2401,6 +2401,12 @@ class TestMemleakNoVar : public TestFixture {
24012401
" return static_cast<int*>(malloc(size));\n"
24022402
"}");
24032403
ASSERT_EQUALS("", errout.str());
2404+
2405+
check("void f() { if (new int[42]) {} }\n" // #10857
2406+
"void g() { if (malloc(42)) {} }\n");
2407+
ASSERT_EQUALS("[test.cpp:1]: (error) Allocation with new, if doesn't release it.\n"
2408+
"[test.cpp:2]: (error) Allocation with malloc, if doesn't release it.\n",
2409+
errout.str());
24042410
}
24052411

24062412
void missingAssignment() {
@@ -2452,7 +2458,7 @@ class TestMemleakNoVar : public TestFixture {
24522458
"{\n"
24532459
" 42,malloc(42);\n"
24542460
"}");
2455-
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function 'malloc' is not stored.\n", "", errout.str());
2461+
ASSERT_EQUALS("[test.cpp:3]: (error) Return value of allocation function 'malloc' is not stored.\n", errout.str());
24562462

24572463
check("void *f()\n"
24582464
"{\n"

0 commit comments

Comments
 (0)