diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index ec57dc026..45fb65ab8 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -176,7 +176,9 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2, if (Token::Match(tok2, "opendir|fdopendir (")) return Dir; - return No; + // User function + const Token *ftok = tokenizer->getFunctionTokenByName(tok2->str().c_str()); + return functionReturnType(ftok); } @@ -430,9 +432,6 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::functionReturnType(const Token *tok) AllocType allocType = getAllocationType(tok2->next(), 0); if (allocType != No) return allocType; - allocType = getReallocationType(tok2->next(), 0); - if (allocType != No) - return allocType; } } @@ -727,21 +726,6 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::listtokAt(-3), "[;{}] %varid% = %var% (", varid)) - { - const Token *ftok = _tokenizer->getFunctionTokenByName(funcname.c_str()); - AllocType a = functionReturnType(ftok); - if (a != No) - { - if (alloctype == No) - alloctype = a; - else if (alloctype != a) - alloctype = Many; - return "alloc"; - } - } - // how many parameters is there in the function call? int numpar = countParameters(tok); if (numpar <= 0) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 6c6ec11b0..ea8c52e91 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -236,6 +236,7 @@ private: TEST_CASE(allocfunc5); TEST_CASE(allocfunc6); TEST_CASE(allocfunc7); + TEST_CASE(allocfunc8); TEST_CASE(throw1); TEST_CASE(throw2); @@ -1971,9 +1972,23 @@ private: "{\n" " char* s = data();\n" "}\n"); - ASSERT_EQUALS(std::string(""), errout.str()); + ASSERT_EQUALS("", errout.str()); } + void allocfunc8() + { + // Ticket #2213 - calling alloc function twice + check("FILE *foo() {\n" + " return fopen(a,b);\n" + "}\n" + "\n" + "void bar() {\n" + " FILE *f = foo();\n" + " f = foo();\n" + " fclose(f);\n" + "}"); + ASSERT_EQUALS("[test.cpp:7]: (error) Resource leak: f\n", errout.str()); + } void throw1()