From 8f88528aa16a3351a1c68634780d99b0a8a02930 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 8 Aug 2023 12:37:40 +0200 Subject: [PATCH 1/7] Add test --- test/cfg/windows.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/cfg/windows.cpp b/test/cfg/windows.cpp index e09e9f073a8..55eb21ecda2 100644 --- a/test/cfg/windows.cpp +++ b/test/cfg/windows.cpp @@ -1113,4 +1113,8 @@ void invalidPrintfArgType_StructMember(double d) { // #9672 my_struct_t my_struct; // cppcheck-suppress invalidPrintfArgType_sint my_struct.st.Format("%d", d); +} + +BOOL MyEnableWindow(HWND hWnd, BOOL bEnable) { + return EnableWindow(hWnd, bEnable); } \ No newline at end of file From 4d7473b1b1eccefcb23fd11fade399ca0d231b97 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 8 Aug 2023 12:38:25 +0200 Subject: [PATCH 2/7] Fix argument direction in windows.cfg --- cfg/windows.cfg | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/cfg/windows.cfg b/cfg/windows.cfg index b4d4d003907..48c1e9f2f84 100644 --- a/cfg/windows.cfg +++ b/cfg/windows.cfg @@ -2960,7 +2960,7 @@ HFONT CreateFont( - + @@ -2985,7 +2985,7 @@ HFONT CreateFont( - + @@ -3023,7 +3023,7 @@ HFONT CreateFont( _In_ LPARAM lParam); --> false - + @@ -3044,7 +3044,7 @@ HFONT CreateFont( _In_ LPARAM lParam); --> false - + @@ -3306,7 +3306,7 @@ HFONT CreateFont( false - + @@ -3315,10 +3315,10 @@ HFONT CreateFont( false - + - + @@ -3327,7 +3327,7 @@ HFONT CreateFont( false - + @@ -3339,7 +3339,7 @@ HFONT CreateFont( false - + From 9ed6a4fc2f46c13fd9c7a9d617be8824b98c3c9c Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 8 Aug 2023 18:37:00 +0200 Subject: [PATCH 3/7] Add missing Qt macros --- cfg/qt.cfg | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cfg/qt.cfg b/cfg/qt.cfg index ee0d314ce9b..cae153acf1e 100644 --- a/cfg/qt.cfg +++ b/cfg/qt.cfg @@ -5097,6 +5097,9 @@ + + + From ea59b5ba8f771d0abc0c52250a0f5b0909c2ffed Mon Sep 17 00:00:00 2001 From: chrchr Date: Wed, 9 Aug 2023 16:16:45 +0200 Subject: [PATCH 4/7] Fix #11866 FN memleak when pointer is converted to bool --- lib/checkmemoryleak.cpp | 26 +++++++++++++++++++++++--- test/testmemleak.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index b6f4dde5b41..93fbb3a4bb3 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -968,11 +968,13 @@ void CheckMemoryLeakNoVar::checkForUnreleasedInputArgument(const Scope *scope) if (Token::simpleMatch(tok->next()->astParent(), "(")) // passed to another function continue; - if (!tok->isKeyword() && (mSettings->library.isNotLibraryFunction(tok) || !mSettings->library.isLeakIgnore(functionName))) + if (!tok->isKeyword() && !tok->function() && !mSettings->library.isLeakIgnore(functionName)) continue; const std::vector args = getArguments(tok); + int argnr = -1; for (const Token* arg : args) { + ++argnr; if (arg->isOp() && !(tok->isKeyword() && arg->str() == "*")) // e.g. switch (*new int) continue; while (arg->astOperand1()) { @@ -983,10 +985,28 @@ void CheckMemoryLeakNoVar::checkForUnreleasedInputArgument(const Scope *scope) const AllocType alloc = getAllocationType(arg, 0); if (alloc == No) continue; - if ((alloc == New || alloc == NewArray) && arg->next() && !(arg->next()->isStandardType() || mSettings->library.detectContainerOrIterator(arg))) - continue; + if (alloc == New || alloc == NewArray) { + const Token* typeTok = arg->next(); + bool bail = !typeTok->isStandardType() && + !mSettings->library.detectContainerOrIterator(typeTok) && + !mSettings->library.podtype(typeTok->expressionString()); + if (bail && typeTok->type() && typeTok->type()->classScope && + typeTok->type()->classScope->numConstructors == 0 && + typeTok->type()->classScope->getDestructor() == nullptr) { + bail = false; + } + if (bail) + continue; + } if (isReopenStandardStream(arg)) continue; + if (tok->function()) { + const Variable* argvar = tok->function()->getArgumentVar(argnr); + if (!argvar || !argvar->valueType()) + continue; + if (argvar->valueType()->typeSize(mSettings->platform, /*p*/ true) >= mSettings->platform.sizeof_pointer) + continue; + } functionCallLeak(arg, arg->str(), functionName); } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index beedd57b232..00bb96ca6f9 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2456,6 +2456,34 @@ class TestMemleakNoVar : public TestFixture { " s->p = s->p ? strcpy(new char[N], s->p) : nullptr;\n" "};\n"); ASSERT_EQUALS("", errout.str()); + + check("struct S {};\n" // #11866 + "void f(bool b);\n" + "void g() {\n" + " f(new int());\n" + " f(new std::vector());\n" + " f(new S());\n" + " f(new tm());\n" + " f(malloc(sizeof(S)));\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4]: (error) Allocation with new, f doesn't release it.\n" + "[test.cpp:5]: (error) Allocation with new, f doesn't release it.\n" + "[test.cpp:6]: (error) Allocation with new, f doesn't release it.\n" + "[test.cpp:7]: (error) Allocation with new, f doesn't release it.\n" + "[test.cpp:8]: (error) Allocation with malloc, f doesn't release it.\n", + errout.str()); + + check("void f(uintptr_t u);\n" + "void g() {\n" + " f((uintptr_t)new int());\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("void f(uint8_t u);\n" + "void g() {\n" + " f((uint8_t)new int());\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void missingAssignment() { From 027deb62c4ad262d5a971f547a8bd448909d0c0b Mon Sep 17 00:00:00 2001 From: chrchr Date: Wed, 9 Aug 2023 16:17:03 +0200 Subject: [PATCH 5/7] Fix argument direction --- cfg/windows.cfg | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cfg/windows.cfg b/cfg/windows.cfg index 48c1e9f2f84..105ee469b56 100644 --- a/cfg/windows.cfg +++ b/cfg/windows.cfg @@ -3699,7 +3699,7 @@ HFONT CreateFont( false - + @@ -3746,7 +3746,7 @@ HFONT CreateFont( false - + From c99e69d7c0165f4ece29db0bfe7d3335bb071707 Mon Sep 17 00:00:00 2001 From: chrchr Date: Wed, 9 Aug 2023 17:05:25 +0200 Subject: [PATCH 6/7] Fix test --- test/testmemleak.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 00bb96ca6f9..552fd52f47f 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2483,7 +2483,8 @@ class TestMemleakNoVar : public TestFixture { "void g() {\n" " f((uint8_t)new int());\n" "}\n"); - ASSERT_EQUALS("", errout.str()); + ASSERT_EQUALS("[test.cpp:3]: (error) Allocation with new, f doesn't release it.\n", + errout.str()); } void missingAssignment() { From 6d577302d470450a9d02043412feee3451a38f3f Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Wed, 9 Aug 2023 22:17:46 +0200 Subject: [PATCH 7/7] Add test --- test/testmemleak.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 552fd52f47f..131116f5198 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2485,6 +2485,14 @@ class TestMemleakNoVar : public TestFixture { "}\n"); ASSERT_EQUALS("[test.cpp:3]: (error) Allocation with new, f doesn't release it.\n", errout.str()); + + check("void f(int i, T t);\n" + "void g(int i, U* u);\n" + "void h() {\n" + " f(1, new int());\n" + " g(1, new int());\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void missingAssignment() {