From fd3cb2497364d350632c288ce3771738499f718e Mon Sep 17 00:00:00 2001 From: Rikard Falkeborn Date: Fri, 23 Aug 2019 06:33:00 +0200 Subject: [PATCH] leakNoReturnVar: Don't break early (#2095) There seems to be no reason for stopping checking the scope if a call to free() is seen (or fclose() or realloc()), so just continue checking. Also, if there are multiple arguments, check all, perhaps there are more memory leaks to warn about. --- lib/checkmemoryleak.cpp | 3 +-- test/testmemleak.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index a756e0693..a0ffc1eaf 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1000,7 +1000,7 @@ void CheckMemoryLeakNoVar::checkForUnreleasedInputArgument(const Scope *scope) functionName == "free" || functionName == "fclose" || functionName == "realloc") - break; + continue; if (!CheckMemoryLeakInFunction::test_white_list(functionName, mSettings, mTokenizer->isCPP())) continue; @@ -1016,7 +1016,6 @@ void CheckMemoryLeakNoVar::checkForUnreleasedInputArgument(const Scope *scope) if (isReopenStandardStream(arg)) continue; functionCallLeak(arg, arg->str(), functionName); - break; } } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index c4554d28e..17342d1ea 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2159,6 +2159,18 @@ private: " return ret;\n" "}"); ASSERT_EQUALS("", errout.str()); + + check("void f() {\n" + " free(malloc(1));\n" + " strcpy(a, strdup(p));\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (error) Allocation with strdup, strcpy doesn't release it.\n", errout.str()); + + check("void f() {\n" + " memcmp(calloc(10, 10), strdup(q), 100);\n" + "}"); + ASSERT_EQUALS("[test.cpp:2]: (error) Allocation with calloc, memcmp doesn't release it.\n" + "[test.cpp:2]: (error) Allocation with strdup, memcmp doesn't release it.\n", errout.str()); } void missingAssignment() {