Fix FPs memleak with array and ptr to ptr (#4139)

* Fix #11019 FN memleak with redundant pointer op

* Style

* Fix #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
This commit is contained in:
chrchr-github 2022-05-26 15:27:36 +02:00 committed by GitHub
parent 86763b7b0a
commit 19dd59eae6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 2 deletions

View File

@ -725,7 +725,9 @@ void CheckMemoryLeakStructMember::check()
const SymbolDatabase* symbolDatabase = mTokenizer->getSymbolDatabase();
for (const Variable* var : symbolDatabase->variableList()) {
if (!var || (!var->isLocal() && !(var->isArgument() && var->scope())) || var->isStatic() || var->isReference())
if (!var || (!var->isLocal() && !(var->isArgument() && var->scope())) || var->isStatic())
continue;
if (var->isReference() || (var->valueType() && var->valueType()->pointer > 1))
continue;
if (var->typeEndToken()->isStandardType())
continue;
@ -752,7 +754,7 @@ bool CheckMemoryLeakStructMember::isMalloc(const Variable *variable)
void CheckMemoryLeakStructMember::checkStructVariable(const Variable * const variable)
{
// Is struct variable a pointer?
if (variable->isPointer()) {
if (variable->isArrayOrPointer()) {
// Check that variable is allocated with malloc
if (!isMalloc(variable))
return;

View File

@ -1930,6 +1930,22 @@ private:
" s->a[e] = strdup(n);\n"
"}\n", false);
ASSERT_EQUALS("", errout.str());
check("void f(struct S** s, const char* c) {\n"
" *s = malloc(sizeof(struct S));\n"
" (*s)->value = strdup(c);\n"
"}\n", false);
ASSERT_EQUALS("", errout.str());
check("struct S {\n"
" size_t mpsz;\n"
" void* hdr;\n"
"};\n"
"void f(struct S s[static 1U], int fd, size_t size) {\n"
" s->mpsz = size;\n"
" s->hdr = mmap(NULL, s->mpsz, PROT_READ, MAP_SHARED, fd, 0);\n"
"}\n", false);
ASSERT_EQUALS("", errout.str());
}
void failedAllocation() {