From e7e2439347c0ea19a4222b0ec413e7c7d269440e Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 15 Feb 2022 14:28:19 +0100 Subject: [PATCH] Fix #8126 unsafeClassCanLeak missing for array of pointers (#3832) --- lib/checkmemoryleak.cpp | 2 +- test/testmemleak.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index a04394682..d7ab0d98a 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -538,7 +538,7 @@ void CheckMemoryLeakInClass::check() // only check classes and structures for (const Scope * scope : symbolDatabase->classAndStructScopes) { for (const Variable &var : scope->varlist) { - if (!var.isStatic() && var.isPointer()) { + if (!var.isStatic() && (var.isPointer() || var.isPointerArray())) { // allocation but no deallocation of private variables in public function.. const Token *tok = var.typeStartToken(); // Either it is of standard type or a non-derived type diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index dc31e0179..a105740f1 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -521,6 +521,7 @@ private: TEST_CASE(class24); // ticket #3806 - false positive in copy constructor TEST_CASE(class25); // ticket #4367 - false positive implementation for destructor is not seen TEST_CASE(class26); // ticket #10789 + TEST_CASE(class27); // ticket #8126 TEST_CASE(staticvar); @@ -1461,6 +1462,17 @@ private: ASSERT_EQUALS("[test.cpp:5]: (style) Class 'S' is unsafe, 'S::p' can leak by wrong usage.\n", errout.str()); } + void class27() { // ticket #8126 - array of pointers + check("struct S {\n" + " S() {\n" + " for (int i = 0; i < 1; i++)\n" + " a = new char[3];\n" + " }\n" + " char* a;\n" + "};\n"); + ASSERT_EQUALS("[test.cpp:6]: (style) Class 'S' is unsafe, 'S::a' can leak by wrong usage.\n", errout.str()); + } + void staticvar() { check("class A\n" "{\n"