From b98e92c5059be64ceccca68f550a783b45656229 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Tue, 28 Apr 2026 21:14:06 +0200 Subject: [PATCH] Fix #14706 FP Memleak struct member when struct is passed to function --- 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 7ce49292ffd..82973958847 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -787,7 +787,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Variable* const vari bool deallocated = false; const Token* const end = tok->linkAt(1); for (const Token* tok2 = tok; tok2 != end; tok2 = tok2->next()) { - if (Token::Match(tok2, "[(,] &| %varid% [,)]", structid)) { + if (Token::Match(tok2, "%varid%", structid)) { /** @todo check if the function deallocates the memory */ deallocated = true; break; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 08ea1530f90..3b7777f4f87 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -1684,6 +1684,7 @@ class TestMemleakStructMember : public TestFixture { TEST_CASE(function3); // #3024: kernel list TEST_CASE(function4); // #3038: Deallocating in function TEST_CASE(function5); // #10381, #10382, #10158 + TEST_CASE(function6); // Handle if-else TEST_CASE(ifelse); @@ -2082,6 +2083,17 @@ class TestMemleakStructMember : public TestFixture { ASSERT_EQUALS("", errout_str()); } + void function6() { + check("struct S { int* p; };\n" // #14706 + "void g(void*);\n" + "void f() {\n" + " S* s = new S();\n" + " s->p = new int();\n" + " g((void*)s);\n" + "}"); + ASSERT_EQUALS("", errout_str()); + } + void ifelse() { check("static void foo()\n" "{\n"