From d34a77884832742c0bf063025a7b32169242a09c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 21 Jul 2011 13:33:20 +0200 Subject: [PATCH] Fixed #2848 (False positive: Memory leak when assigning and freeing from another struct) --- lib/checkmemoryleak.cpp | 2 +- test/testmemleak.cpp | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 4f985b044..9a557af47 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -3185,7 +3185,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Token * const vartok --parlevel; } - if (Token::Match(tok4, "[(,] %varid% [,)]", structid)) + if (Token::Match(tok4, "[(,] &| %varid% [,)]", structid)) { /** @todo check if the function deallocates the memory */ deallocated = true; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index abb9d7ec9..96a87665c 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -4918,8 +4918,8 @@ private: // Failed allocation TEST_CASE(failedAllocation); - // Deallocating in a function - TEST_CASE(function); + TEST_CASE(function1); // Deallocating in function + TEST_CASE(function2); // #2848: Taking address in function // Handle if-else TEST_CASE(ifelse); @@ -5073,7 +5073,7 @@ private: ASSERT_EQUALS("", errout.str()); } - void function() + void function1() { // Not found function => assume that the function may deallocate check("static void foo()\n" @@ -5093,6 +5093,17 @@ private: ASSERT_EQUALS("", errout.str()); } + // #2848: Taking address in function 'assign' + void function2() + { + check("void f() {\n" + " A a = { 0 };\n" + " a.foo = (char *) malloc(10);\n" + " assign(&a);\n" + "}\n", "test.c"); + ASSERT_EQUALS("", errout.str()); + } + void ifelse() { check("static void foo()\n"