Skip to content

Commit cdeebc1

Browse files
Fix #11155 FN: leakNoVarFunctionCall (ternary operator) (#4239)
* Fix #10857 FN: leakNoVarFunctionCall * Fix TODO * Fix #10858 FN: leakNoVarFunctionCall (if ( b && malloc ) ) * #11155 FN: leakNoVarFunctionCall (ternary operator)
1 parent e7e23e8 commit cdeebc1

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

lib/checkmemoryleak.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1070,9 +1070,14 @@ void CheckMemoryLeakNoVar::checkForUnusedReturnValue(const Scope *scope)
10701070
if (closingBrace->str() == "}" && Token::Match(closingBrace->link()->tokAt(-1), "%name%") && (!isNew && precedes(tok, closingBrace->link())))
10711071
continue;
10721072
returnValueNotUsedError(tok, tok->str());
1073-
} else if (Token::Match(parent, "%comp%|!|,|%oror%|&&")) {
1074-
if (!(parent->astParent() && parent->str() == ","))
1075-
returnValueNotUsedError(tok, tok->str());
1073+
} else if (Token::Match(parent, "%comp%|!|,|%oror%|&&|:")) {
1074+
if (parent->astParent() && parent->str() == ",")
1075+
continue;
1076+
if (parent->str() == ":") {
1077+
if (!(Token::simpleMatch(parent->astParent(), "?") && !parent->astParent()->astParent()))
1078+
continue;
1079+
}
1080+
returnValueNotUsedError(tok, tok->str());
10761081
}
10771082
}
10781083
}

test/testmemleak.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2607,6 +2607,14 @@ class TestMemleakNoVar : public TestFixture {
26072607
ASSERT_EQUALS("[test.cpp:1]: (error) Return value of allocation function 'malloc' is not stored.\n"
26082608
"[test.cpp:2]: (error) Return value of allocation function 'malloc' is not stored.\n",
26092609
errout.str());
2610+
2611+
check("void f0(const bool b) { b ? new int : nullptr; }\n" // #11155
2612+
"void f1(const bool b) { b ? nullptr : new int; }\n"
2613+
"int* g0(const bool b) { return b ? new int : nullptr; }\n"
2614+
"void g1(const bool b) { h(b, b ? nullptr : new int); }\n");
2615+
ASSERT_EQUALS("[test.cpp:1]: (error) Return value of allocation function 'new' is not stored.\n"
2616+
"[test.cpp:2]: (error) Return value of allocation function 'new' is not stored.\n",
2617+
errout.str());
26102618
}
26112619

26122620
void smartPointerFunctionParam() {

0 commit comments

Comments
 (0)