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

@ -89,7 +89,7 @@ static AllocType GetDeallocationType( const TOKEN *tok, const char *varnames[] )
tok = gettok( tok, 4 ); tok = gettok( tok, 4 );
if ( Match(tok,"{") ) if ( Match(tok,"{") )
tok = tok->next; tok = tok->next;
} }
if ( Match(tok, "delete %var1% ;", varnames) ) if ( Match(tok, "delete %var1% ;", varnames) )
return New; return New;
@ -167,75 +167,92 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
// Check subfunction... // Check subfunction...
if (Alloc != No && Match(tok,"[{};] %var% (")) if (Alloc != No && Match(tok,"[{};] %var% ("))
{ {
AllocType dealloc = GetDeallocationType(tok->next, varnames);
const char *funcname = getstr( tok, 1 ); 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] == ';' ) unsigned int param = 0;
break; for (const TOKEN *tok2 = gettok(tok,2); tok2; tok2 = tok2->next)
if ( tok2->str[0] == ',' )
param++;
if ( Match(tok2, "[(,] %var1% [,)]", varnames) )
{ {
// Find function.. if ( tok2->str[0] == ';' || tok2->str[0] == ')' )
const TOKEN *ftok = GetFunctionTokenByName( funcname );
ftok = gettok(ftok,2);
if ( ! ftok )
break; break;
if ( tok2->str[0] == ',' )
param++;
// Goto function parameter.. if ( Match(tok2, "[(,] %var1% [,)]", varnames) )
for ( unsigned int fparam = 0; ftok && fparam < param; ftok = ftok->next )
{ {
if ( ftok->str[0] == ',' ) // Find function..
++fparam; const TOKEN *ftok = GetFunctionTokenByName( funcname );
} ftok = gettok(ftok,2);
for ( ; ftok; ftok = ftok->next ) if ( ! ftok )
{
if ( ! Match(ftok,"%var% [,)]") )
continue;
const char *paramname[2] = {0};
paramname[0] = ftok->str;
// parse function and check if it deallocates the parameter..
int _indentlevel = 0;
while (_indentlevel>=0 && ftok)
{ {
if ( ftok->str[0] == '{' ) // Can't find the function but to avoid false
_indentlevel++; // positives it is assumed that the variable is
else if ( ftok->str[0] == '}' ) // deallocated..
{
_indentlevel--;
if ( _indentlevel <= 0 )
break;
}
if ( _indentlevel >= 1 ) // 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 )
{ {
AllocType dealloc = GetDeallocationType(ftok,paramname); if ( ftok->str[0] == ',' )
if ( dealloc != No ) ++fparam;
}
for ( ; ftok; ftok = ftok->next )
{
if ( ! Match(ftok,"%var% [,)]") )
continue;
const char *paramname[2] = {0};
paramname[0] = ftok->str;
// parse function and check if it deallocates the parameter..
int _indentlevel = 0;
while (_indentlevel>=0 && ftok)
{ {
if ( Alloc != No && Alloc != dealloc ) if ( ftok->str[0] == '{' )
_indentlevel++;
else if ( ftok->str[0] == '}' )
{ {
MismatchError( Tok1, varname ); _indentlevel--;
return; if ( _indentlevel <= 0 )
break;
} }
// Deallocated at same indentlevel as the allocation => no memory leak if ( _indentlevel >= 1 )
if ( alloc_indentlevel == indentlevel ) {
return; AllocType dealloc = GetDeallocationType(ftok,paramname);
if ( dealloc != No )
{
if ( Alloc != No && Alloc != dealloc )
{
MismatchError( Tok1, varname );
return;
}
dealloc_indentlevel = indentlevel; // Deallocated at same indentlevel as the allocation => no memory leak
break; if ( alloc_indentlevel == indentlevel )
return;
dealloc_indentlevel = indentlevel;
break;
}
}
ftok = ftok->next;
} }
break;
} }
ftok = ftok->next;
} }
break; break;
} }
break;
} }
} }
} }

View File

@ -777,6 +777,14 @@ static void memleak_in_function()
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, code, "[test.cpp:6]: Memory leak: p\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, "" );
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------