Fixed #2213 (false negative: resource leak (calling allocating subfunction twice))

This commit is contained in:
Daniel Marjamäki 2011-03-19 14:05:22 +01:00
parent 31af3a64bc
commit 2dcd0128b0
2 changed files with 19 additions and 20 deletions

View File

@ -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::list<co
return ret;
}
// 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());
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)

View File

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