From 917496a8448a17f3368731e2a9382f8268735fee Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 25 Aug 2022 22:52:51 +0200 Subject: [PATCH] Fix #5678 FN destructor with no definition hide mismatching alloc/dealloc (#4401) * Fix #5678 FN destructor with no definition hide mismatching allocation / deallocation * Format * Fix test * Format --- lib/checkmemoryleak.cpp | 8 ++++---- test/testmemleak.cpp | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 7c0013abe..be1c508e2 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -632,14 +632,14 @@ void CheckMemoryLeakInClass::variable(const Scope *scope, const Token *tokVarnam if (destructor) deallocInDestructor = true; - // several types of allocation/deallocation? - if (memberDealloc != CheckMemoryLeak::No && memberDealloc != dealloc) - dealloc = CheckMemoryLeak::Many; - if (dealloc != CheckMemoryLeak::Many && memberAlloc != CheckMemoryLeak::No && memberAlloc != Many && memberAlloc != dealloc) { mismatchAllocDealloc({tok}, classname + "::" + varname); } + // several types of allocation/deallocation? + if (memberDealloc != CheckMemoryLeak::No && memberDealloc != dealloc) + dealloc = CheckMemoryLeak::Many; + memberDealloc = dealloc; } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index df9539cbd..1dd2b8d22 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -1571,6 +1571,18 @@ private: " delete [] pkt_buffer;\n" "}"); ASSERT_EQUALS("[test.cpp:14]: (error) Mismatching allocation and deallocation: A::pkt_buffer\n", errout.str()); + + check("struct S {\n" // 5678 + " ~S();\n" + " void f();\n" + " int* p;\n" + "};\n" + "void S::f() {\n" + " p = new char[1];\n" + " delete p;\n" + " p = 0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:8]: (error) Mismatching allocation and deallocation: S::p\n", errout.str()); } void mismatch2() { // #5659