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:
parent
86763b7b0a
commit
19dd59eae6
|
@ -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;
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue