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()); }