Skip to content

Commit 7f1eba2

Browse files
Fix #11184 FN leakNoVarFunctionCall with assignment (#8567)
1 parent e0ee4f2 commit 7f1eba2

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

lib/checkmemoryleak.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,10 +1016,8 @@ void CheckMemoryLeakNoVarImpl::checkForUnreleasedInputArgument(const Scope *scop
10161016
const Token* tok2 = tok->next()->astParent();
10171017
while (tok2 && (tok2->isCast() || Token::Match(tok2, "?|:")))
10181018
tok2 = tok2->astParent();
1019-
if (Token::Match(tok2, "%assign%")) // TODO: check if function returns allocated resource
1020-
continue;
1021-
if (Token::simpleMatch(tok->astTop(), "return"))
1022-
continue;
1019+
const bool hasAssign = Token::Match(tok2, "%assign%");
1020+
const bool hasReturn = Token::simpleMatch(tok->astTop(), "return");
10231021

10241022
const std::string& functionName = tok->str();
10251023
if ((tok->isCpp() && functionName == "delete") ||
@@ -1031,6 +1029,12 @@ void CheckMemoryLeakNoVarImpl::checkForUnreleasedInputArgument(const Scope *scop
10311029
if (!tok->isKeyword() && !tok->function() && !mSettings.library.isLeakIgnore(functionName))
10321030
continue;
10331031

1032+
if ((hasAssign || hasReturn) && !tok->function()) {
1033+
const std::string& ret = mSettings->library.returnValueType(tok);
1034+
if (ret.empty() || endsWith(ret, "*"))
1035+
continue;
1036+
}
1037+
10341038
const std::vector<const Token *> args = getArguments(tok);
10351039
int argnr = -1;
10361040
for (const Token* arg : args) {

test/testmemleak.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2602,6 +2602,17 @@ class TestMemleakNoVar : public TestFixture {
26022602
" a.g(new int);\n"
26032603
"}\n");
26042604
ASSERT_EQUALS("", errout_str());
2605+
2606+
check("int f() {\n" // #11184
2607+
" return strlen(new char[4]{});\n"
2608+
"}\n"
2609+
"int g() {\n"
2610+
" int i = strlen(new char[4]{});\n"
2611+
" return i;\n"
2612+
"}\n");
2613+
ASSERT_EQUALS("[test.cpp:2:19]: (error) Allocation with new, strlen doesn't release it. [leakNoVarFunctionCall]\n"
2614+
"[test.cpp:5:20]: (error) Allocation with new, strlen doesn't release it. [leakNoVarFunctionCall]\n",
2615+
errout_str());
26052616
}
26062617

26072618
void missingAssignment() {

0 commit comments

Comments
 (0)