From b90a726441299becac5d62d5cf344c0e5a1fadf5 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 29 Jun 2022 07:22:26 +0200 Subject: [PATCH] Fix FP leakNoVarFunctionCall (#4241) * Fix #10857 FN: leakNoVarFunctionCall * Fix TODO * Fix #10858 FN: leakNoVarFunctionCall (if ( b && malloc ) ) * #11155 FN: leakNoVarFunctionCall (ternary operator) * Fix #11157 FN: leakNoVarFunctionCall (switch condition) * Fix FN constStatement * Fix FP leakNoVarFunctionCall * Fix FP leakNoVarFunctionCall * Format --- lib/checkmemoryleak.cpp | 4 +++- test/testmemleak.cpp | 8 ++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index e6d3af291..cbb2a20ef 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -998,7 +998,9 @@ void CheckMemoryLeakNoVar::checkForUnreleasedInputArgument(const Scope *scope) const Token* tok2 = tok->next()->astParent(); while (tok2 && tok2->isCast()) tok2 = tok2->astParent(); - if (Token::Match(tok2, "%assign%|return")) + if (Token::Match(tok2, "%assign%")) + continue; + if (Token::simpleMatch(tok->astTop(), "return")) continue; const std::string& functionName = tok->str(); diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index e45b8a9d0..176482c7b 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2407,6 +2407,14 @@ private: ASSERT_EQUALS("[test.cpp:1]: (error) Allocation with new, if doesn't release it.\n" "[test.cpp:2]: (error) Allocation with malloc, if doesn't release it.\n", errout.str()); + + check("const char* string(const char* s) {\n" + " StringSet::iterator it = strings_.find(s);\n" + " if (it != strings_.end())\n" + " return *it;\n" + " return *strings_.insert(it, std::strcpy(new char[std::strlen(s) + 1], s));\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void missingAssignment() {