Use library to get deallocation function (#5061)

* Use library to get deallocation function

* Add suppressions

* Amend

* Remove suppression

* More getDeallocFuncInfo()

* Fix suppressions

* Fix suppression
This commit is contained in:
chrchr-github 2023-05-21 14:01:14 +02:00 committed by GitHub
parent d30f8e18f7
commit e621f721fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 17 additions and 7 deletions

View File

@ -1738,7 +1738,7 @@ bool CheckClass::hasAllocation(const Function *func, const Scope* scope, const T
// check for deallocating memory
const Token *var;
if (Token::Match(tok, "free ( %var%"))
if (Token::Match(tok, "%name% ( %var%") && mSettings->library.getDeallocFuncInfo(tok))
var = tok->tokAt(2);
else if (Token::Match(tok, "delete [ ] %var%"))
var = tok->tokAt(3);

View File

@ -876,7 +876,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable * const var
}
// Deallocating the struct..
else if (Token::Match(tok3, "free|kfree ( %varid% )", structid)) {
else if (Token::Match(tok3, "%name% ( %varid% )", structid) && mSettings->library.getDeallocFuncInfo(tok3)) {
if (indentlevel2 == 0)
memoryLeak(tok3, variable->name() + "." + tok2->strAt(2), Malloc);
break;
@ -910,7 +910,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable * const var
--indentlevel4;
if (indentlevel4 == 0)
break;
} else if (Token::Match(tok4, "free|kfree ( %var% . %varid% )", structmemberid)) {
} else if (Token::Match(tok4, "%name% ( %var% . %varid% )", structmemberid) && mSettings->library.getDeallocFuncInfo(tok4)) {
break;
}
}

View File

@ -2323,7 +2323,7 @@ void CheckOther::checkInvalidFree()
// If a variable that was previously assigned a newly-allocated memory location is
// added or subtracted from when used to free the memory, report an error.
else if (Token::Match(tok, "free|g_free|delete ( %any% +|-") ||
else if ((Token::Match(tok, "%name% ( %any% +|-") && mSettings->library.getDeallocFuncInfo(tok)) ||
Token::Match(tok, "delete [ ] ( %any% +|-") ||
Token::Match(tok, "delete %any% +|- %any%")) {

View File

@ -837,7 +837,7 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const
}
}
// Freeing memory (not considered "using" the pointer if it was also allocated in this function)
if (Token::Match(tok, "free|g_free|kfree|vfree ( %var% )") ||
if ((Token::Match(tok, "%name% ( %var% )") && mSettings->library.getDeallocFuncInfo(tok)) ||
(mTokenizer->isCPP() && (Token::Match(tok, "delete %var% ;") || Token::Match(tok, "delete [ ] %var% ;")))) {
nonneg int varid = 0;
if (tok->str() != "delete") {

View File

@ -51,6 +51,7 @@ void validCode(int argInt, GHashTableIter * hash_table_iter, GHashTable * hash_t
printf("%s", pGchar1);
g_free(pGchar1);
// cppcheck-suppress unusedAllocatedMemory
GError * pGerror = g_error_new(1, -2, "a %d", 1);
g_error_free(pGerror);

View File

@ -1287,6 +1287,7 @@ void dl(const char* libname, const char* func)
void* sym = dlsym(lib, funcname);
// cppcheck-suppress ignoredReturnValue
dlsym(lib, "foo");
// cppcheck-suppress unassignedVariable
void* uninit;
// cppcheck-suppress uninitvar
dlclose(uninit);

View File

@ -25,8 +25,10 @@ void validCode(PyObject * pPyObjArg)
Py_CLEAR(pPyObjNULL);
(void)PyErr_NewException("text", NULL, NULL);
// cppcheck-suppress unusedAllocatedMemory
char * pBuf1 = PyMem_Malloc(5);
PyMem_Free(pBuf1);
// cppcheck-suppress unusedAllocatedMemory
int * pIntBuf1 = PyMem_New(int, 10);
PyMem_Free(pIntBuf1);
}

View File

@ -648,6 +648,7 @@ void uninitvar_clearerr(void)
void uninitvar_fclose(void)
{
// cppcheck-suppress unassignedVariable
FILE *fp;
// cppcheck-suppress uninitvar
fclose(fp);

View File

@ -1795,7 +1795,6 @@ void uninitvar_fread(void)
void uninitvar_free(void)
{
// cppcheck-suppress unassignedVariable
void *block;
// cppcheck-suppress uninitvar
std::free(block);

View File

@ -258,6 +258,7 @@ void validCode()
CloseHandle(event);
}
// cppcheck-suppress unusedAllocatedMemory
void *pMem1 = _malloca(1);
_freea(pMem1);
// Memory from _alloca must not be freed
@ -343,7 +344,7 @@ void validCode()
SecureZeroMemory(byteBuf, sizeof(byteBuf));
RtlFillMemory(byteBuf, sizeof(byteBuf), 0xff);
// cppcheck-suppress LocalAllocCalled
// cppcheck-suppress [LocalAllocCalled, unusedAllocatedMemory]
HLOCAL pLocalAlloc = LocalAlloc(1, 2);
LocalFree(pLocalAlloc);
@ -357,6 +358,7 @@ void validCode()
__noop(1, "test", NULL);
__nop();
// cppcheck-suppress unusedAllocatedMemory
void * pAlloc1 = _aligned_malloc(100, 2);
_aligned_free(pAlloc1);
@ -437,6 +439,7 @@ void mismatchAllocDealloc()
// cppcheck-suppress mismatchAllocDealloc
free(pChar);
// cppcheck-suppress unusedAllocatedMemory
pChar = _malloca(32);
// cppcheck-suppress mismatchAllocDealloc
_aligned_free(pChar);
@ -751,6 +754,7 @@ void invalidFunctionArg()
void uninitvar()
{
// cppcheck-suppress unassignedVariable
HANDLE hSemaphore;
// cppcheck-suppress uninitvar
CloseHandle(hSemaphore);
@ -765,12 +769,14 @@ void uninitvar()
// cppcheck-suppress uninitvar
lstrcat(buf, buf2);
// cppcheck-suppress unassignedVariable
HANDLE hMutex1, hMutex2;
// cppcheck-suppress uninitvar
ReleaseMutex(hMutex1);
// cppcheck-suppress uninitvar
CloseHandle(hMutex2);
// cppcheck-suppress unassignedVariable
HANDLE hEvent1, hEvent2, hEvent3, hEvent4;
// cppcheck-suppress uninitvar
PulseEvent(hEvent1);