CheckMemoryLeak: better handling of switch blocks

This commit is contained in:
Daniel Marjamäki 2008-08-14 06:41:26 +00:00
parent 46be988e9a
commit 6b12d31f5f
2 changed files with 30 additions and 1 deletions

View File

@ -135,6 +135,7 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
int alloc_indentlevel = -1;
int dealloc_indentlevel = -1;
std::list<int> loop_indentlevel;
std::list<int> switch_indentlevel;
bool isif = false;
@ -157,6 +158,9 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
if ( !loop_indentlevel.empty() && indentlevel <= loop_indentlevel.back() )
loop_indentlevel.pop_back();
if ( !switch_indentlevel.empty() && indentlevel <= switch_indentlevel.back() )
switch_indentlevel.pop_back();
if ( indentlevel < alloc_indentlevel )
alloc_indentlevel = -1;
@ -261,6 +265,10 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
if ( Match(tok,"while") || Match(tok,"for") )
loop_indentlevel.push_back( indentlevel );
// switch..
if (Match(tok,"switch"))
switch_indentlevel.push_back( indentlevel );
// Skip stuff like: if (!var) ...
if ( Match(tok, "if ( ! %var1% )", varnames) ||
Match(tok, "if ( unlikely ( ! %var1% ) )", varnames) ||
@ -378,6 +386,7 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[]
// continue/break loop..
if (Alloc != No &&
loop_indentlevel.empty() &&
switch_indentlevel.empty() &&
(Match(tok,"continue") || Match(tok,"break")))
{
MemoryLeak( tok, varname );

View File

@ -460,7 +460,8 @@ static void memleak_in_function()
// There are 2 sections:
// * Simple testcases
// * if else
// * for/while..
// * for/while
// * switch
// * mismatching allocation and deallocation
// * garbage collection
// * arrays
@ -649,6 +650,25 @@ static void memleak_in_function()
////////////////////////////////////////////////
// switch
////////////////////////////////////////////////
code = "void f()\n"
"{\n"
" char *str = new char[10];\n"
" switch (abc)\n"
" {\n"
" case 1:\n"
" break;\n"
" };\n"
" delete [] str;\n"
"}\n";
check( CheckMemoryLeak, __LINE__, code, "" );
////////////////////////////////////////////////