From 0ba3d259178503adeb9a8413126398b9e61508a0 Mon Sep 17 00:00:00 2001 From: PKEuS Date: Fri, 27 Nov 2015 11:04:18 +0100 Subject: [PATCH] CheckMemoryLeak: Correctly detect new char[...]() as array allocation (#7164) --- lib/checkmemoryleak.cpp | 2 +- test/testmemleak.cpp | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 60ddde8c6..dd069a9de 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -123,7 +123,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2, return Malloc; if (tokenizer->isCPP() && tok2->str() == "new") { - if (tok2->astOperand1() && tok2->astOperand1()->str() == "[") + if (tok2->astOperand1() && (tok2->astOperand1()->str() == "[" || (tok2->astOperand1()->astOperand1() && tok2->astOperand1()->astOperand1()->str() == "["))) return NewArray; return New; } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 75c9895b3..ba8abbb14 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -1420,6 +1420,12 @@ private: " free(a);\n" "}\n", false, false, true); ASSERT_EQUALS("[test.cpp:4]: (error) Mismatching allocation and deallocation: a\n", errout.str()); + + check("void f() {\n" + " char* str = new char[42]();\n" + " delete[] str;\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void mismatch2() {