diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index a4dc0ee4d..43c0664e2 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -764,6 +764,8 @@ void CheckMemoryLeakStructMember::check() continue; if (var->typeEndToken()->isStandardType()) continue; + if (var->scope()->hasInlineOrLambdaFunction()) + continue; checkStructVariable(var); } } diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 07af526d9..0dcceb21d 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -4447,6 +4447,8 @@ bool Scope::hasInlineOrLambdaFunction() const // Lambda function if (s->type == Scope::eLambda) return true; + if (s->hasInlineOrLambdaFunction()) + return true; } return false; } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index f77661372..b70b35921 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -1677,6 +1677,8 @@ private: TEST_CASE(varid_2); // #5315: Analysis confused by ((variable).attribute) notation TEST_CASE(customAllocation); + + TEST_CASE(lambdaInForLoop); // #9793 } void err() { @@ -2062,6 +2064,22 @@ private: "}", false); ASSERT_EQUALS("[test.c:7]: (error) Memory leak: abc.a\n", errout.str()); } + + void lambdaInForLoop() { // #9793 + check( + "struct S { int * p{nullptr}; };\n" + "int main()\n" + "{\n" + " S s;\n" + " s.p = new int[10];\n" + " for (int i = 0; i < 10; ++i) {\n" + " s.p[i] = []() { return 1; }();\n" + " }\n" + " delete[] s.p;\n" + " return 0;\n" + "}", true); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestMemleakStructMember)