From 6b12d31f5f82459c6b833071cdeb1d76a67e6191 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Thu, 14 Aug 2008 06:41:26 +0000 Subject: [PATCH] CheckMemoryLeak: better handling of switch blocks --- CheckMemoryLeak.cpp | 9 +++++++++ tests.cpp | 22 +++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index fcbf18138..7ab13e380 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -135,6 +135,7 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] int alloc_indentlevel = -1; int dealloc_indentlevel = -1; std::list loop_indentlevel; + std::list 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 ); diff --git a/tests.cpp b/tests.cpp index ea802f033..c653edcbc 100644 --- a/tests.cpp +++ b/tests.cpp @@ -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, "" ); + + + + ////////////////////////////////////////////////