From ae27b613aed047e8fddfe59705571f6e0168f030 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 2 Dec 2023 14:15:10 +0100 Subject: [PATCH] Fix #12239 FP memleak when passing this in allocation (#5719) --- 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() {