From 5e5b8d386155e8a8cf258d7efe88d8229cdde263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 24 Apr 2010 20:40:57 +0200 Subject: [PATCH] Fixed #1146 (improve check: memory leak not detected (allocation in subfunction)) --- lib/checkmemoryleak.cpp | 6 ++++++ test/testmemleak.cpp | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index f309dcf89..b005e65a8 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -423,6 +423,12 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Token *tok) continue; } + if (tok->str() == ";") + { + tok = tok->next(); + continue; + } + if (tok->str() == "return") { if (varid > 0 && Token::Match(tok->next(), "%varid% ;", varid)) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 9a48e5195..60b2947bc 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -295,6 +295,7 @@ private: TEST_CASE(allocfunc1); TEST_CASE(allocfunc2); + TEST_CASE(allocfunc3); TEST_CASE(throw1); TEST_CASE(throw2); @@ -1666,6 +1667,19 @@ private: ASSERT_EQUALS(std::string(""), errout.str()); } + void allocfunc3() + { + check("static char *a()\n" + "{\n" + " char *data = malloc(10);;" + " return data;\n" + "}\n" + "static void b()\n" + "{\n" + " char *p = a();\n" + "}\n"); + ASSERT_EQUALS(std::string("[test.cpp:8]: (error) Memory leak: p\n"), errout.str()); + }