From 3d3593e6e73fd28142d3a2f1eb61254efea5ddb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 29 Dec 2010 22:18:23 +0100 Subject: [PATCH] Fixed #2374 (False 'memory leak' report (assigning to map in subfunction)) --- lib/checkmemoryleak.cpp | 8 +++++--- test/testmemleak.cpp | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 2b58c307e..f9d1cb00b 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -443,14 +443,16 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Token *tok) { allocType = getReallocationType(tok->tokAt(2), varid); } - if (allocType != No) - return allocType; + } + if (Token::Match(tok, "= %varid% ;", varid)) + { + return No; } if (tok->str() == "return") return allocType; } - return No; + return allocType; } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 9c83032b7..e8ae658f6 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -234,6 +234,7 @@ private: TEST_CASE(allocfunc4); TEST_CASE(allocfunc5); TEST_CASE(allocfunc6); + TEST_CASE(allocfunc7); TEST_CASE(throw1); TEST_CASE(throw2); @@ -1924,6 +1925,24 @@ private: } + void allocfunc7() + { + // Ticket #2374 - no false positive + check("char *data()\n" + "{\n" + " char *s = malloc(100);\n" + " strings[0] = s;\n" + " return s;\n" + "}\n" + "\n" + "static void foo()\n" + "{\n" + " char* s = data();\n" + "}\n"); + ASSERT_EQUALS(std::string(""), errout.str()); + } + + void throw1() {