This commit is contained in:
Sebastien Debrard 2011-03-19 15:39:54 +01:00
commit 14a5e9a071
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 (")) if (Token::Match(tok2, "opendir|fdopendir ("))
return Dir; 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); AllocType allocType = getAllocationType(tok2->next(), 0);
if (allocType != No) if (allocType != No)
return allocType; 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; 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? // how many parameters is there in the function call?
int numpar = countParameters(tok); int numpar = countParameters(tok);
if (numpar <= 0) if (numpar <= 0)

View File

@ -236,6 +236,7 @@ private:
TEST_CASE(allocfunc5); TEST_CASE(allocfunc5);
TEST_CASE(allocfunc6); TEST_CASE(allocfunc6);
TEST_CASE(allocfunc7); TEST_CASE(allocfunc7);
TEST_CASE(allocfunc8);
TEST_CASE(throw1); TEST_CASE(throw1);
TEST_CASE(throw2); TEST_CASE(throw2);
@ -1971,9 +1972,23 @@ private:
"{\n" "{\n"
" char* s = data();\n" " char* s = data();\n"
"}\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() void throw1()