Fix #388 (resource leak not detected, allocation through function call)
http://sourceforge.net/apps/trac/cppcheck/ticket/388
This commit is contained in:
parent
2e69d50458
commit
ab762b1a00
|
@ -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;
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue