From 907218254ea5e9df3a81b16539c1587656859183 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 15 Feb 2022 20:03:02 +0100 Subject: [PATCH] #8126 Fix previous commit (#3833) * Fix #8126 unsafeClassCanLeak missing for array of pointers * #8126 Fix previous commit * Format * Format --- lib/checkmemoryleak.cpp | 8 ++++++-- test/testclass.cpp | 16 ++++++++++++++++ test/testmemleak.cpp | 6 +++--- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index d7ab0d98a..2afab311e 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -592,7 +592,7 @@ void CheckMemoryLeakInClass::variable(const Scope *scope, const Token *tokVarnam } // Allocate.. - if (!body || Token::Match(tok, "%varid% =", varid)) { + if (!body || Token::Match(tok, "%varid% =|[", varid)) { // var1 = var2 = ... // bail out if (tok->strAt(-1) == "=") @@ -604,7 +604,11 @@ void CheckMemoryLeakInClass::variable(const Scope *scope, const Token *tokVarnam tok->strAt(-2) != scope->className) return; - AllocType alloc = getAllocationType(tok->tokAt(body ? 2 : 3), 0); + const Token* allocTok = tok->tokAt(body ? 2 : 3); + if (tok->astParent() && tok->astParent()->str() == "[" && tok->astParent()->astParent()) + allocTok = tok->astParent()->astParent()->astOperand2(); + + AllocType alloc = getAllocationType(allocTok, 0); if (alloc != CheckMemoryLeak::No) { if (constructor) allocInConstructor = true; diff --git a/test/testclass.cpp b/test/testclass.cpp index 17d0fbde3..58449c261 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -84,6 +84,7 @@ private: TEST_CASE(copyConstructor3); // defaulted/deleted TEST_CASE(copyConstructor4); // base class with private constructor TEST_CASE(copyConstructor5); // multiple inheritance + TEST_CASE(copyConstructor6); // array of pointers TEST_CASE(noOperatorEq); // class with memory management should have operator eq TEST_CASE(noDestructor); // class with memory management should have destructor @@ -967,6 +968,21 @@ private: ASSERT_EQUALS("", errout.str()); } + void copyConstructor6() { + checkCopyConstructor("struct S {\n" + " S() {\n" + " for (int i = 0; i < 5; i++)\n" + " a[i] = new char[3];\n" + " }\n" + " char* a[5];\n" + "};\n"); + TODO_ASSERT_EQUALS("[test.cpp:4]: (warning) Struct 'S' does not have a copy constructor which is recommended since it has dynamic memory/resource allocation(s).\n" + "[test.cpp:4]: (warning) Struct 'S' does not have a operator= which is recommended since it has dynamic memory/resource allocation(s).\n" + "[test.cpp:4]: (warning) Struct 'S' does not have a destructor which is recommended since it has dynamic memory/resource allocation(s).\n", + "", + errout.str()); + } + void noOperatorEq() { checkCopyConstructor("struct F {\n" " char* c;\n" diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index a105740f1..bcc21d410 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -1465,10 +1465,10 @@ private: 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" + " for (int i = 0; i < 5; i++)\n" + " a[i] = new char[3];\n" " }\n" - " char* a;\n" + " char* a[5];\n" "};\n"); ASSERT_EQUALS("[test.cpp:6]: (style) Class 'S' is unsafe, 'S::a' can leak by wrong usage.\n", errout.str()); }