From ab762b1a00dde3ae849d9b1c62825ab310d942ae Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Tue, 6 Oct 2009 23:14:32 +0300 Subject: [PATCH] Fix #388 (resource leak not detected, allocation through function call) http://sourceforge.net/apps/trac/cppcheck/ticket/388 --- src/checkmemoryleak.cpp | 6 ++---- test/testmemleak.cpp | 43 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 8522f6420..75ed487fa 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -414,10 +414,8 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Token *tok) { if (varid > 0 && Token::Match(tok->next(), "%varid% ;", varid)) return allocType; - if (Token::Match(tok, "return new %type% ;")) - return New; - if (Token::Match(tok, "return new %type% [ %any% ] ;")) - return NewArray; + + return getAllocationType(tok->next(), varid); } return No; diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 86af54184..e65732205 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -1391,6 +1391,26 @@ private: " char *p = a();\n" "}\n"); ASSERT_EQUALS(std::string("[test.cpp:8]: (error) Memory leak: p\n"), errout.str()); + + check("FILE *a()\n" + "{\n" + " return fopen(\"test.txt\",\"w\");\n" + "}\n" + "static void b()\n" + "{\n" + " FILE *p = a();\n" + "}\n"); + ASSERT_EQUALS(std::string("[test.cpp:8]: (error) Memory leak: p\n"), errout.str()); + + check("char *a()\n" + "{\n" + " return malloc(10);\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()); } void allocfunc2() @@ -1406,6 +1426,29 @@ private: " delete [] p;\n" "}\n"); ASSERT_EQUALS(std::string(""), errout.str()); + + check("char *a(char *a)\n" + "{\n" + " return realloc(a, 10);\n" + "}\n" + "static void b()\n" + "{\n" + " char *p = a(0);\n" + " char *p = a(p);\n" + " free(p);\n" + "}\n"); + ASSERT_EQUALS(std::string(""), errout.str()); + + check("char *a()\n" + "{\n" + " return malloc(10);\n" + "}\n" + "static void b()\n" + "{\n" + " char *p = a();\n" + " free(p);\n" + "}\n"); + ASSERT_EQUALS(std::string(""), errout.str()); }