Skip to content

Commit 19dd59e

Browse files
Fix FPs memleak with array and ptr to ptr (cppcheck-opensource#4139)
* Fix #11019 FN memleak with redundant pointer op * Style * Fix cppcheck-opensource#7705 FN: Memory leak not detected on struct member * Fix FP memleak with function call, fix cppcheckError * Fix FP memleak with array * Fix FPs memleak with array and ptr to ptr
1 parent 86763b7 commit 19dd59e

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

lib/checkmemoryleak.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,9 @@ void CheckMemoryLeakStructMember::check()
725725

726726
const SymbolDatabase* symbolDatabase = mTokenizer->getSymbolDatabase();
727727
for (const Variable* var : symbolDatabase->variableList()) {
728-
if (!var || (!var->isLocal() && !(var->isArgument() && var->scope())) || var->isStatic() || var->isReference())
728+
if (!var || (!var->isLocal() && !(var->isArgument() && var->scope())) || var->isStatic())
729+
continue;
730+
if (var->isReference() || (var->valueType() && var->valueType()->pointer > 1))
729731
continue;
730732
if (var->typeEndToken()->isStandardType())
731733
continue;
@@ -752,7 +754,7 @@ bool CheckMemoryLeakStructMember::isMalloc(const Variable *variable)
752754
void CheckMemoryLeakStructMember::checkStructVariable(const Variable * const variable)
753755
{
754756
// Is struct variable a pointer?
755-
if (variable->isPointer()) {
757+
if (variable->isArrayOrPointer()) {
756758
// Check that variable is allocated with malloc
757759
if (!isMalloc(variable))
758760
return;

test/testmemleak.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,22 @@ class TestMemleakStructMember : public TestFixture {
19301930
" s->a[e] = strdup(n);\n"
19311931
"}\n", false);
19321932
ASSERT_EQUALS("", errout.str());
1933+
1934+
check("void f(struct S** s, const char* c) {\n"
1935+
" *s = malloc(sizeof(struct S));\n"
1936+
" (*s)->value = strdup(c);\n"
1937+
"}\n", false);
1938+
ASSERT_EQUALS("", errout.str());
1939+
1940+
check("struct S {\n"
1941+
" size_t mpsz;\n"
1942+
" void* hdr;\n"
1943+
"};\n"
1944+
"void f(struct S s[static 1U], int fd, size_t size) {\n"
1945+
" s->mpsz = size;\n"
1946+
" s->hdr = mmap(NULL, s->mpsz, PROT_READ, MAP_SHARED, fd, 0);\n"
1947+
"}\n", false);
1948+
ASSERT_EQUALS("", errout.str());
19331949
}
19341950

19351951
void failedAllocation() {

0 commit comments

Comments
 (0)