From 8390c6ff2f11db393bc2278738d2ac4d77e22f22 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 2 Dec 2023 12:36:02 +0100 Subject: [PATCH] Fix #12239 FP memleak when passing this in allocation --- lib/checkleakautovar.cpp | 2 +- test/testleakautovar.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/checkleakautovar.cpp b/lib/checkleakautovar.cpp index 61079e36b8f..2f9f5909dd4 100644 --- a/lib/checkleakautovar.cpp +++ b/lib/checkleakautovar.cpp @@ -263,7 +263,7 @@ static bool isLocalVarNoAutoDealloc(const Token *varTok, const bool isCpp) // non-pod variable if (isCpp) { // Possibly automatically deallocated memory - if (isAutoDealloc(var) && Token::Match(varTok, "%var% = new")) + if (isAutoDealloc(var) && Token::Match(varTok, "%var% [=({] new")) return false; if (!var->isPointer() && !var->typeStartToken()->isStandardType()) return false; diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 09891cf6631..6ca90726d4d 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -580,6 +580,20 @@ class TestLeakAutoVar : public TestFixture { ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: p\n" "[test.cpp:4]: (error) Memory leak: q\n", errout.str()); + + check("struct S : B {\n" // #12239 + " void f();\n" + " void g();\n" + "};\n" + "void S::f() {\n" + " FD* fd(new FD(this));\n" + " fd->exec();\n" + "}\n" + "void S::g() {\n" + " FD* fd{ new FD(this) };\n" + " fd->exec();\n" + "}\n", true); + ASSERT_EQUALS("", errout.str()); } void isAutoDealloc() {