From dbc5929fa27fb10f4297a5125d9ece1bbd301d5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 4 Feb 2011 20:41:35 +0100 Subject: [PATCH] Fixed #2503 (false positive: memory leak (struct member, address is taken)) --- lib/checkmemoryleak.cpp | 2 +- test/testmemleak.cpp | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) 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"