From 2150995475f007e58d6406f415727064be35c012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 8 Aug 2011 19:58:25 +0200 Subject: [PATCH] Fixed #2981 (False positive: struct member leak) --- lib/checkmemoryleak.cpp | 6 ++++++ test/testmemleak.cpp | 13 ++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index c77a5759d..b8c09b87c 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -3161,6 +3161,12 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Token * const vartok break; } + // struct assignment.. + else if (Token::Match(tok3, "= %varid% ;", structid)) + { + break; + } + // goto isn't handled well.. bail out even though there might be leaks else if (tok3->str() == "goto") break; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 78a9f162f..d0299746b 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -4962,6 +4962,7 @@ private: // assignments TEST_CASE(assign1); TEST_CASE(assign2); + TEST_CASE(assign3); // Failed allocation TEST_CASE(failedAllocation); @@ -5100,11 +5101,21 @@ private: { check("static void foo() {\n" " struct ABC *abc = malloc(123);\n" - " abc->a = abc->b = malloc(10)\n" + " abc->a = abc->b = malloc(10);\n" "}\n"); ASSERT_EQUALS("", errout.str()); } + void assign3() + { + check("void f(struct s *f1) {\n" + " struct s f2;\n" + " f2.a = malloc(100);\n" + " *f1 = f2;\n" + "}\n", "test.c"); + ASSERT_EQUALS("", errout.str()); + } + void failedAllocation() { check("static struct ABC * foo()\n"