From 46be988e9a6e7c55876d5266cc8117f84f88933f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 13 Aug 2008 19:08:24 +0000 Subject: [PATCH] CheckMemoryLeak: Handle one more test case (return pointer) --- CheckMemoryLeak.cpp | 11 +++++++++-- tests.cpp | 12 ++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index ec0a12cf1..fcbf18138 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -385,7 +385,7 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] } // Return without deallocating the memory.. - if ( Alloc != No && alloc_indentlevel >= 0 && dealloc_indentlevel <= 0 && Match(tok, "return") ) + if ( Alloc != No && (indentlevel==0 || (alloc_indentlevel >= 0 && dealloc_indentlevel <= 0)) && Match(tok, "return") ) { bool retvar = false; for ( const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next ) @@ -435,8 +435,15 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] } } - if ( indentlevel <= alloc_indentlevel ) + if ( indentlevel == 0 ) return; + + if ( indentlevel <= alloc_indentlevel ) + { + Alloc = No; + alloc_indentlevel = -1; + dealloc_indentlevel = -1; + } } } } diff --git a/tests.cpp b/tests.cpp index 32858211d..ea802f033 100644 --- a/tests.cpp +++ b/tests.cpp @@ -595,6 +595,18 @@ static void memleak_in_function() check( CheckMemoryLeak, __LINE__, code, "[test.cpp:8]: Memory leak: s\n" ); + code = "static char *f()\n" + "{\n" + " char *s;\n" + " if ( abc )\n" + " {\n" + " s = new char[10];\n" + " }\n" + " return s;\n" + "}\n"; + check( CheckMemoryLeak, __LINE__, code, "" ); + +