Fixed ticket #349 (Add detection for resource leaks after tmpfile() usage)

https://apps.sourceforge.net/trac/cppcheck/ticket/349
This commit is contained in:
Slava Semushin 2009-06-01 17:40:24 +07:00
parent 09fce76e30
commit 37a485f4f0
2 changed files with 48 additions and 1 deletions

View File

@ -109,7 +109,7 @@ CheckMemoryLeakClass::AllocType CheckMemoryLeakClass::GetAllocationType(const To
if (Token::Match(tok2, "new %type% [")) if (Token::Match(tok2, "new %type% ["))
return NewArray; return NewArray;
if (Token::simpleMatch(tok2, "fopen (")) if (Token::Match(tok2, "fopen|tmpfile ("))
return File; return File;
if (Token::simpleMatch(tok2, "popen (")) if (Token::simpleMatch(tok2, "popen ("))

View File

@ -214,6 +214,7 @@ private:
TEST_CASE(stdstring); TEST_CASE(stdstring);
TEST_CASE(strndup_function); TEST_CASE(strndup_function);
TEST_CASE(tmpfile_function);
TEST_CASE(fcloseall_function); TEST_CASE(fcloseall_function);
TEST_CASE(file_functions); TEST_CASE(file_functions);
@ -2186,6 +2187,45 @@ private:
ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: out\n", errout.str()); ASSERT_EQUALS("[test.cpp:4]: (error) Memory leak: out\n", errout.str());
} }
void tmpfile_function()
{
check("void f()\n"
"{\n"
" FILE *f = tmpfile();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (error) Resource leak: f\n", errout.str());
check("void f()\n"
"{\n"
" FILE *f = tmpfile();\n"
" if (!f)\n"
" return;\n"
"}\n");
ASSERT_EQUALS("[test.cpp:6]: (error) Resource leak: f\n", errout.str());
check("void f()\n"
"{\n"
" FILE *f = tmpfile();\n"
" fclose(f);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("void f()\n"
"{\n"
" FILE *f = tmpfile();\n"
" if (!f)\n"
" return;\n"
" fclose(f);\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("FILE *f()\n"
"{\n"
" return tmpfile();\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void fcloseall_function() void fcloseall_function()
{ {
check("void f()\n" check("void f()\n"
@ -2194,6 +2234,13 @@ private:
" fcloseall();\n" " fcloseall();\n"
"}\n"); "}\n");
ASSERT_EQUALS(std::string(""), errout.str()); ASSERT_EQUALS(std::string(""), errout.str());
check("void f()\n"
"{\n"
" FILE *f = tmpfile();\n"
" fcloseall();\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
} }
void opendir_function() void opendir_function()