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() {