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)
This commit is contained in:
chrchr-github 2022-06-27 20:55:09 +02:00 committed by GitHub
parent e7e23e87c2
commit cdeebc15ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 3 deletions

View File

@ -1070,9 +1070,14 @@ void CheckMemoryLeakNoVar::checkForUnusedReturnValue(const Scope *scope)
if (closingBrace->str() == "}" && Token::Match(closingBrace->link()->tokAt(-1), "%name%") && (!isNew && precedes(tok, closingBrace->link()))) if (closingBrace->str() == "}" && Token::Match(closingBrace->link()->tokAt(-1), "%name%") && (!isNew && precedes(tok, closingBrace->link())))
continue; continue;
returnValueNotUsedError(tok, tok->str()); returnValueNotUsedError(tok, tok->str());
} else if (Token::Match(parent, "%comp%|!|,|%oror%|&&")) { } else if (Token::Match(parent, "%comp%|!|,|%oror%|&&|:")) {
if (!(parent->astParent() && parent->str() == ",")) if (parent->astParent() && parent->str() == ",")
returnValueNotUsedError(tok, tok->str()); continue;
if (parent->str() == ":") {
if (!(Token::simpleMatch(parent->astParent(), "?") && !parent->astParent()->astParent()))
continue;
}
returnValueNotUsedError(tok, tok->str());
} }
} }
} }

View File

@ -2607,6 +2607,14 @@ private:
ASSERT_EQUALS("[test.cpp:1]: (error) Return value of allocation function 'malloc' is not stored.\n" ASSERT_EQUALS("[test.cpp:1]: (error) Return value of allocation function 'malloc' is not stored.\n"
"[test.cpp:2]: (error) Return value of allocation function 'malloc' is not stored.\n", "[test.cpp:2]: (error) Return value of allocation function 'malloc' is not stored.\n",
errout.str()); errout.str());
check("void f0(const bool b) { b ? new int : nullptr; }\n" // #11155
"void f1(const bool b) { b ? nullptr : new int; }\n"
"int* g0(const bool b) { return b ? new int : nullptr; }\n"
"void g1(const bool b) { h(b, b ? nullptr : new int); }\n");
ASSERT_EQUALS("[test.cpp:1]: (error) Return value of allocation function 'new' is not stored.\n"
"[test.cpp:2]: (error) Return value of allocation function 'new' is not stored.\n",
errout.str());
} }
void smartPointerFunctionParam() { void smartPointerFunctionParam() {