From 15e965b530628496907ab6b76fcb6cb45662e528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 26 Aug 2011 19:26:21 +0200 Subject: [PATCH] Fixed #3038 (False Positive - memory leak (struct member passed to subfunction)) --- lib/checkmemoryleak.cpp | 3 +-- test/testmemleak.cpp | 13 +++++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index a491afa3f..2df5549fe 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -3209,8 +3209,7 @@ void CheckMemoryLeakStructMember::checkStructVariable(const Token * const vartok break; } - // Linux kernel list - if (Token::Match(tok4, "[(,] & %varid% . %var% [,)]", structid)) + if (Token::Match(tok4, "[(,] &| %varid% . %var% [,)]", structid)) { /** @todo check if the function deallocates the memory */ deallocated = true; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 5dcc63287..be2bc763d 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -4994,6 +4994,7 @@ private: TEST_CASE(function1); // Deallocating in function TEST_CASE(function2); // #2848: Taking address in function TEST_CASE(function3); // #3024: kernel list + TEST_CASE(function4); // #3038: Deallocating in function // Handle if-else TEST_CASE(ifelse); @@ -5199,6 +5200,18 @@ private: ASSERT_EQUALS("", errout.str()); } + // #3038: deallocating in function + void function4() + { + check("void a(char *p) { char *x = p; free(x); }\n" + "void b() {\n" + " struct ABC abc;\n" + " abc.a = (char *) malloc(10);\n" + " a(abc.a);\n" + "}\n", "test.c"); + ASSERT_EQUALS("", errout.str()); + } + void ifelse() { check("static void foo()\n"