From 5dcf217dc37e9d8ea37d8ffaa2408f80897234fd Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 22 Mar 2024 14:25:01 +0100 Subject: [PATCH] Fix FN unusedAllocatedMemory (f'up to #12332) (#6158) See https://github.com/danmar/cppcheck/pull/6154 --- cfg/opencv2.cfg | 2 +- lib/library.cpp | 6 ++++++ test/cfg/opencv2.cpp | 9 ++++++--- test/cfg/std.cpp | 6 +++--- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/cfg/opencv2.cfg b/cfg/opencv2.cfg index 797035c1e57..80b51ad7a08 100644 --- a/cfg/opencv2.cfg +++ b/cfg/opencv2.cfg @@ -69,7 +69,7 @@ false - + diff --git a/lib/library.cpp b/lib/library.cpp index b90f07bc6a7..15ba68c31c6 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -1075,6 +1075,8 @@ bool Library::isuninitargbad(const Token *ftok, int argnr, int indirect, bool *h /** get allocation info for function */ const Library::AllocFunc* Library::getAllocFuncInfo(const Token *tok) const { + while (Token::simpleMatch(tok, "::")) + tok = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1(); const std::string funcname = getFunctionName(tok); return isNotLibraryFunction(tok) && functions.find(funcname) != functions.end() ? nullptr : getAllocDealloc(mAlloc, funcname); } @@ -1082,6 +1084,8 @@ const Library::AllocFunc* Library::getAllocFuncInfo(const Token *tok) const /** get deallocation info for function */ const Library::AllocFunc* Library::getDeallocFuncInfo(const Token *tok) const { + while (Token::simpleMatch(tok, "::")) + tok = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1(); const std::string funcname = getFunctionName(tok); return isNotLibraryFunction(tok) && functions.find(funcname) != functions.end() ? nullptr : getAllocDealloc(mDealloc, funcname); } @@ -1089,6 +1093,8 @@ const Library::AllocFunc* Library::getDeallocFuncInfo(const Token *tok) const /** get reallocation info for function */ const Library::AllocFunc* Library::getReallocFuncInfo(const Token *tok) const { + while (Token::simpleMatch(tok, "::")) + tok = tok->astOperand2() ? tok->astOperand2() : tok->astOperand1(); const std::string funcname = getFunctionName(tok); return isNotLibraryFunction(tok) && functions.find(funcname) != functions.end() ? nullptr : getAllocDealloc(mRealloc, funcname); } diff --git a/test/cfg/opencv2.cpp b/test/cfg/opencv2.cpp index 847ab1b42f4..de0f3731b81 100644 --- a/test/cfg/opencv2.cpp +++ b/test/cfg/opencv2.cpp @@ -30,7 +30,8 @@ void validCode(const char* argStr) cvStr += " World"; std::cout << cvStr; - char * pBuf = (char *)cv::fastMalloc(20); // cppcheck-suppress cstyleCast + // cppcheck-suppress [cstyleCast, unusedAllocatedMemory] + char * pBuf = (char *)cv::fastMalloc(20); cv::fastFree(pBuf); } @@ -42,7 +43,9 @@ void ignoredReturnValue() void memleak() { - const char * pBuf = (char *)cv::fastMalloc(1000); // cppcheck-suppress cstyleCast - std::cout << pBuf; // cppcheck-suppress valueFlowBailoutIncompleteVar + // cppcheck-suppress cstyleCast + const char * pBuf = (char *)cv::fastMalloc(1000); + // cppcheck-suppress [uninitdata, valueFlowBailoutIncompleteVar] + std::cout << pBuf; // cppcheck-suppress memleak } diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index 8f434451499..b2108d2d515 100644 --- a/test/cfg/std.cpp +++ b/test/cfg/std.cpp @@ -2928,7 +2928,7 @@ void uninitvar_longjmp(void) void uninitvar_malloc(void) { size_t size; - // cppcheck-suppress [uninitvar, cstyleCast] + // cppcheck-suppress [uninitvar, cstyleCast, unusedAllocatedMemory] int *p = (int*)std::malloc(size); free(p); } @@ -4957,7 +4957,7 @@ void std_vector_data_arithmetic() void memleak_std_malloc() // #12332 { - //cppcheck-suppress [unreadVariable, constVariablePointer] + //cppcheck-suppress [unreadVariable, constVariablePointer, unusedAllocatedMemory] void* p = std::malloc(1); //cppcheck-suppress memleak } @@ -4971,7 +4971,7 @@ void memleak_std_realloc(void* block, size_t newsize) void unusedAllocatedMemory_std_free() { - // TODO cppcheck-suppress unusedAllocatedMemory + // cppcheck-suppress unusedAllocatedMemory void* p = std::malloc(1); std::free(p); }