diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index f98a9c014..409381a25 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -2999,7 +2999,7 @@ void CheckMemoryLeakStructMember::check() break; // Struct member is allocated => check if it is also properly deallocated.. - else if (Token::Match(tok2, "%varid% . %var% = malloc|strdup|kmalloc (", vartok->varId())) + else if (Token::Match(tok2->previous(), "[;{}] %varid% . %var% = malloc|strdup|kmalloc (", vartok->varId())) { const unsigned int structid(vartok->varId()); const unsigned int structmemberid(tok2->tokAt(2)->varId()); diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 6699e56ac..713179a00 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -4253,7 +4253,8 @@ private: TEST_CASE(ret2); // assignments - TEST_CASE(assign); + TEST_CASE(assign1); + TEST_CASE(assign2); // Failed allocation TEST_CASE(failedAllocation); @@ -4356,7 +4357,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void assign() + void assign1() { check("static void foo()\n" "{\n" @@ -4385,6 +4386,15 @@ private: } + void assign2() + { + check("static void foo() {\n" + " struct ABC *abc = malloc(123);\n" + " abc->a = abc->b = malloc(10)\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void failedAllocation() { check("static struct ABC * foo()\n"