Skip to content

Commit fd3cb24

Browse files
rikardfalkeborndanmar
authored andcommitted
leakNoReturnVar: Don't break early (danmar#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.
1 parent c0a8d62 commit fd3cb24

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lib/checkmemoryleak.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,7 +1000,7 @@ void CheckMemoryLeakNoVar::checkForUnreleasedInputArgument(const Scope *scope)
10001000
functionName == "free" ||
10011001
functionName == "fclose" ||
10021002
functionName == "realloc")
1003-
break;
1003+
continue;
10041004

10051005
if (!CheckMemoryLeakInFunction::test_white_list(functionName, mSettings, mTokenizer->isCPP()))
10061006
continue;
@@ -1016,7 +1016,6 @@ void CheckMemoryLeakNoVar::checkForUnreleasedInputArgument(const Scope *scope)
10161016
if (isReopenStandardStream(arg))
10171017
continue;
10181018
functionCallLeak(arg, arg->str(), functionName);
1019-
break;
10201019
}
10211020

10221021
}

test/testmemleak.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,6 +2159,18 @@ class TestMemleakNoVar : public TestFixture {
21592159
" return ret;\n"
21602160
"}");
21612161
ASSERT_EQUALS("", errout.str());
2162+
2163+
check("void f() {\n"
2164+
" free(malloc(1));\n"
2165+
" strcpy(a, strdup(p));\n"
2166+
"}");
2167+
ASSERT_EQUALS("[test.cpp:3]: (error) Allocation with strdup, strcpy doesn't release it.\n", errout.str());
2168+
2169+
check("void f() {\n"
2170+
" memcmp(calloc(10, 10), strdup(q), 100);\n"
2171+
"}");
2172+
ASSERT_EQUALS("[test.cpp:2]: (error) Allocation with calloc, memcmp doesn't release it.\n"
2173+
"[test.cpp:2]: (error) Allocation with strdup, memcmp doesn't release it.\n", errout.str());
21622174
}
21632175

21642176
void missingAssignment() {

0 commit comments

Comments
 (0)