Fixed #1790 (mismatching allocation/deallocation false positive)

This commit is contained in:
Daniel Marjamäki 2010-06-30 09:21:15 +02:00
parent 5ea28ccbba
commit 3205775eb9
2 changed files with 21 additions and 1 deletions

View File

@ -694,6 +694,7 @@ const char * CheckMemoryLeakInFunction::call_func(const Token *tok, std::list<co
} }
// Check if this is a function that allocates memory.. // Check if this is a function that allocates memory..
if (Token::Match(tok->tokAt(-3), "[;{}] %varid% = %var% (", varid))
{ {
const Token *ftok = _tokenizer->getFunctionTokenByName(funcname.c_str()); const Token *ftok = _tokenizer->getFunctionTokenByName(funcname.c_str());
AllocType a = functionReturnType(ftok); AllocType a = functionReturnType(ftok);

View File

@ -298,6 +298,7 @@ private:
TEST_CASE(allocfunc3); TEST_CASE(allocfunc3);
TEST_CASE(allocfunc4); TEST_CASE(allocfunc4);
TEST_CASE(allocfunc5); TEST_CASE(allocfunc5);
TEST_CASE(allocfunc6);
TEST_CASE(throw1); TEST_CASE(throw1);
TEST_CASE(throw2); TEST_CASE(throw2);
@ -1808,10 +1809,28 @@ private:
" free(tmp);\n" " free(tmp);\n"
"}\n"); "}\n");
TODO_ASSERT_EQUALS(std::string(""), errout.str()); TODO_ASSERT_EQUALS(std::string(""), errout.str());
} }
void allocfunc6()
{
check("static FILE* data()\n"
"{\n"
" return fopen(\"data.txt\",\"rt\");\n"
"}\n"
"\n"
"static void foo()\n"
"{\n"
" char* expr;\n"
" func(&expr);\n"
"\n"
" FILE *f = data();\n"
" fclose(f);\n"
"\n"
" free(expr);\n"
"}\n");
ASSERT_EQUALS(std::string(""), errout.str());
}