CheckMemoryLeak: Calling unknown function => assume that it's deallocating variable

This commit is contained in:
Daniel Marjamäki 2008-08-13 16:41:27 +00:00
parent ff6adcbeff
commit 0394aedd52
2 changed files with 76 additions and 51 deletions

View File

@ -167,12 +167,15 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
// Check subfunction...
if (Alloc != No && Match(tok,"[{};] %var% ("))
{
const char *funcname = getstr( tok, 1 );
AllocType dealloc = GetDeallocationType(tok->next, varnames);
const char *funcname = getstr( tok, 1 );
if (dealloc==No && strcmp(funcname,"if") && strcmp(funcname,"for") && strcmp(funcname,"while"))
{
unsigned int param = 0;
for (const TOKEN *tok2 = gettok(tok,2); tok2; tok2 = tok2->next)
{
if ( tok2->str[0] == ';' )
if ( tok2->str[0] == ';' || tok2->str[0] == ')' )
break;
if ( tok2->str[0] == ',' )
param++;
@ -183,8 +186,20 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
const TOKEN *ftok = GetFunctionTokenByName( funcname );
ftok = gettok(ftok,2);
if ( ! ftok )
break;
{
// Can't find the function but to avoid false
// positives it is assumed that the variable is
// deallocated..
// Deallocated at same indentlevel as the allocation => no memory leak
if ( alloc_indentlevel == indentlevel )
return;
dealloc_indentlevel = indentlevel;
}
else
{
// Goto function parameter..
for ( unsigned int fparam = 0; ftok && fparam < param; ftok = ftok->next )
{
@ -235,10 +250,12 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
}
break;
}
}
break;
}
}
}
}
// for, while set loop level..
if ( Match(tok,"while") || Match(tok,"for") )

View File

@ -777,6 +777,14 @@ static void memleak_in_function()
"}\n";
check( CheckMemoryLeak, __LINE__, code, "[test.cpp:6]: Memory leak: p\n" );
code = "static void f()\n"
"{\n"
" char *p = new char[100];\n"
" foo(p);\n"
"}\n";
check( CheckMemoryLeak, __LINE__, code, "" );
}
//---------------------------------------------------------------------------