diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 8ba6b4770..0e3f3feb8 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -940,9 +940,11 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const else if (var && var->mType == Variables::pointer && Token::Match(start, "%name% =") && findAllocFuncCallToken(start->next()->astOperand2(), mSettings->library)) { - bool allocate = true; const Token *allocFuncCallToken = findAllocFuncCallToken(start->next()->astOperand2(), mSettings->library); + const Library::AllocFunc *allocFunc = mSettings->library.getAllocFuncInfo(allocFuncCallToken); + + bool allocateMemory = !allocFunc || Library::ismemory(allocFunc->groupId); if (allocFuncCallToken->str() == "new") { const Token *type = allocFuncCallToken->next(); @@ -956,11 +958,11 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const if (!type->isStandardType()) { const Variable *variable = start->variable(); if (!variable || !isRecordTypeWithoutSideEffects(variable->type())) - allocate = false; + allocateMemory = false; } } - if (allocate) + if (allocateMemory) variables.allocateMemory(varid1, tok); else variables.write(varid1, tok); diff --git a/test/cfg/posix.c b/test/cfg/posix.c index 23a0ef44b..de86d771c 100644 --- a/test/cfg/posix.c +++ b/test/cfg/posix.c @@ -197,6 +197,7 @@ void memleak_getaddrinfo() void memleak_mmap(int fd) { + // cppcheck-suppress unusedAllocatedMemory // cppcheck-suppress unreadVariable void *addr = mmap(NULL, 255, PROT_NONE, MAP_PRIVATE, fd, 0); // cppcheck-suppress memleak diff --git a/test/cfg/python.c b/test/cfg/python.c index 011e140b5..34aac8c41 100644 --- a/test/cfg/python.c +++ b/test/cfg/python.c @@ -48,6 +48,7 @@ void PyMem_Malloc_memleak() void PyMem_Malloc_mismatchAllocDealloc() { + // cppcheck-suppress unusedAllocatedMemory char * pBuf1 = PyMem_Malloc(10); // cppcheck-suppress mismatchAllocDealloc free(pBuf1); diff --git a/test/cfg/std.c b/test/cfg/std.c index 49bc0f43f..211ac393b 100644 --- a/test/cfg/std.c +++ b/test/cfg/std.c @@ -87,6 +87,7 @@ void bufferAccessOutOfBounds(void) void memleak_aligned_alloc(void) { + // cppcheck-suppress unusedAllocatedMemory // cppcheck-suppress unreadVariable char * alignedBuf = aligned_alloc(8, 16); // cppcheck-suppress memleak @@ -1118,6 +1119,7 @@ void uninitvar_calloc(void) { size_t nitems; size_t size; + // cppcheck-suppress unusedAllocatedMemory // cppcheck-suppress uninitvar int * p = (int*) calloc(nitems, size); free(p); @@ -2598,6 +2600,7 @@ void uninitvar_longjmp(void) void uninitvar_malloc(void) { size_t size; + // cppcheck-suppress unusedAllocatedMemory // cppcheck-suppress uninitvar int *p = (int*)malloc(size); free(p); diff --git a/test/cfg/windows.cpp b/test/cfg/windows.cpp index 7f0ce21ff..33a3c8bb1 100644 --- a/test/cfg/windows.cpp +++ b/test/cfg/windows.cpp @@ -403,6 +403,7 @@ void nullPointer() void memleak_malloca() { + // cppcheck-suppress unusedAllocatedMemory // cppcheck-suppress unreadVariable void *pMem = _malloca(10); // cppcheck-suppress memleak