From 6f1c790bf39968a62042a93331888f9f7f858300 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 19 Mar 2024 15:13:25 +0100 Subject: [PATCH 1/7] Fix FN unusedAllocatedMemory --- lib/library.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/library.cpp b/lib/library.cpp index b90f07bc6a7..04184a9e1b6 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(); 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(); 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(); const std::string funcname = getFunctionName(tok); return isNotLibraryFunction(tok) && functions.find(funcname) != functions.end() ? nullptr : getAllocDealloc(mRealloc, funcname); } From 52ab62fdfbab49b986153b980d92b4e7b059708b Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 19 Mar 2024 15:42:10 +0100 Subject: [PATCH 2/7] Format --- lib/library.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/library.cpp b/lib/library.cpp index 04184a9e1b6..1561f9c3ff9 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -1076,7 +1076,7 @@ bool Library::isuninitargbad(const Token *ftok, int argnr, int indirect, bool *h const Library::AllocFunc* Library::getAllocFuncInfo(const Token *tok) const { while (Token::simpleMatch(tok, "::")) - tok = tok-> astOperand2(); + tok = tok->astOperand2(); const std::string funcname = getFunctionName(tok); return isNotLibraryFunction(tok) && functions.find(funcname) != functions.end() ? nullptr : getAllocDealloc(mAlloc, funcname); } From 15f4c73b99dfbc3a4460a83ba10193cc52e9efde Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 19 Mar 2024 15:47:06 +0100 Subject: [PATCH 3/7] nullptr check --- lib/library.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/library.cpp b/lib/library.cpp index 1561f9c3ff9..15ba68c31c6 100644 --- a/lib/library.cpp +++ b/lib/library.cpp @@ -1076,7 +1076,7 @@ bool Library::isuninitargbad(const Token *ftok, int argnr, int indirect, bool *h const Library::AllocFunc* Library::getAllocFuncInfo(const Token *tok) const { while (Token::simpleMatch(tok, "::")) - tok = tok->astOperand2(); + 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); } @@ -1085,7 +1085,7 @@ const Library::AllocFunc* Library::getAllocFuncInfo(const Token *tok) const const Library::AllocFunc* Library::getDeallocFuncInfo(const Token *tok) const { while (Token::simpleMatch(tok, "::")) - tok = tok->astOperand2(); + 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); } @@ -1094,7 +1094,7 @@ const Library::AllocFunc* Library::getDeallocFuncInfo(const Token *tok) const const Library::AllocFunc* Library::getReallocFuncInfo(const Token *tok) const { while (Token::simpleMatch(tok, "::")) - tok = tok->astOperand2(); + 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); } From 4dbcfea825f124b00e9ceceb61bcbc23e5e67c93 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 19 Mar 2024 15:50:58 +0100 Subject: [PATCH 4/7] Fix tests --- test/cfg/opencv2.cpp | 7 +++++-- test/cfg/std.cpp | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/test/cfg/opencv2.cpp b/test/cfg/opencv2.cpp index 7af2d6b7055..b1279fef89e 100644 --- a/test/cfg/opencv2.cpp +++ b/test/cfg/opencv2.cpp @@ -28,7 +28,9 @@ 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); + // cppcheck-suppress uninitdata cv::fastFree(pBuf); } @@ -40,7 +42,8 @@ void ignoredReturnValue() void memleak() { - const char * pBuf = (char *)cv::fastMalloc(1000); // cppcheck-suppress cstyleCast + // cppcheck-suppress [cstyleCast, unusedAllocatedMemory] + const char * pBuf = (char *)cv::fastMalloc(1000); std::cout << pBuf; // cppcheck-suppress memleak } diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index d27a87d0b47..0438ea39487 100644 --- a/test/cfg/std.cpp +++ b/test/cfg/std.cpp @@ -2923,7 +2923,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); } @@ -4949,7 +4949,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 } From e992956a96d7dd2e9662bf25b128c3c99d03efa4 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 19 Mar 2024 16:08:33 +0100 Subject: [PATCH 5/7] Fix --- test/cfg/opencv2.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/cfg/opencv2.cpp b/test/cfg/opencv2.cpp index b1279fef89e..e3825ffe90f 100644 --- a/test/cfg/opencv2.cpp +++ b/test/cfg/opencv2.cpp @@ -42,8 +42,9 @@ void ignoredReturnValue() void memleak() { - // cppcheck-suppress [cstyleCast, unusedAllocatedMemory] + // cppcheck-suppress cstyleCast const char * pBuf = (char *)cv::fastMalloc(1000); + // cppcheck-suppress uninitdata std::cout << pBuf; // cppcheck-suppress memleak } From 601e62d9c888d055cba61399ddf5d0e6bffeb38b Mon Sep 17 00:00:00 2001 From: chrchr Date: Thu, 21 Mar 2024 19:00:39 +0100 Subject: [PATCH 6/7] Fix cv::fastFree --- cfg/opencv2.cfg | 2 +- test/cfg/opencv2.cpp | 1 - 2 files changed, 1 insertion(+), 2 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/test/cfg/opencv2.cpp b/test/cfg/opencv2.cpp index e3825ffe90f..6667785ef4e 100644 --- a/test/cfg/opencv2.cpp +++ b/test/cfg/opencv2.cpp @@ -30,7 +30,6 @@ void validCode(const char* argStr) // cppcheck-suppress [cstyleCast, unusedAllocatedMemory] char * pBuf = (char *)cv::fastMalloc(20); - // cppcheck-suppress uninitdata cv::fastFree(pBuf); } From 28bbae10db3ebe9cd614badc4e19a8c7874d61aa Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 22 Mar 2024 12:30:18 +0100 Subject: [PATCH 7/7] Fix test in std.cpp --- test/cfg/std.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/cfg/std.cpp b/test/cfg/std.cpp index a8a0f018c4e..b2108d2d515 100644 --- a/test/cfg/std.cpp +++ b/test/cfg/std.cpp @@ -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); }