CheckMemoryLeak: Improved the checking

(tests.cpp:memleak_in_function:test16)
This commit is contained in:
Daniel Marjamäki 2008-04-12 11:44:32 +00:00
parent 90c2822031
commit 4805f48a4f
2 changed files with 26 additions and 8 deletions

View File

@ -175,22 +175,29 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
if ( Match( tok, "[=,(] %var1% [,);]", varnames ) ) if ( Match( tok, "[=,(] %var1% [,);]", varnames ) )
return; return;
// Return the memory..
if ( Match( tok, "return %var1%", varnames ) )
return;
// Return without deallocating the memory.. // Return without deallocating the memory..
if ( Alloc != No && alloc_indentlevel >= 0 && Match(tok, "return") ) if ( Alloc != No && alloc_indentlevel >= 0 && Match(tok, "return") )
{ {
bool retvar = false;
for ( const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next ) for ( const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next )
{ {
if ( Match( tok2, "%var1%", varnames ) ) if ( Match( tok2, "%var1%", varnames ) )
return; {
if ( tok2->str[0] == ';' ) retvar = true;
break; break;
}
if ( tok2->str[0] == ';' )
{
break;
}
} }
MemoryLeak( tok, varname );
return; if ( ! retvar )
MemoryLeak( tok, varname );
if ( indentlevel <= alloc_indentlevel )
return;
} }
} }
} }

View File

@ -617,6 +617,17 @@ static void memleak_in_function()
"}\n"; "}\n";
check( CheckMemoryLeak, __LINE__, test15, "" ); check( CheckMemoryLeak, __LINE__, test15, "" );
const char test16[] = "static char *f()\n"
"{\n"
" char *s = new char[100];\n"
" if ( a == b )\n"
" {\n"
" return s;\n"
" }\n"
" return NULL;\n"
"}\n";
check( CheckMemoryLeak, __LINE__, test16, "[test.cpp:8]: Memory leak: s\n" );
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------