Fix #388 (resource leak not detected, allocation through function call)

http://sourceforge.net/apps/trac/cppcheck/ticket/388
This commit is contained in:
Reijo Tomperi 2009-10-06 23:14:32 +03:00
parent 2e69d50458
commit ab762b1a00
2 changed files with 45 additions and 4 deletions

View File

@ -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;

View File

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