From 4805f48a4f6c779a16fb7b0150863e84e894a0bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 12 Apr 2008 11:44:32 +0000 Subject: [PATCH] CheckMemoryLeak: Improved the checking (tests.cpp:memleak_in_function:test16) --- CheckMemoryLeak.cpp | 23 +++++++++++++++-------- tests.cpp | 11 +++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index 6ab3bc10f..cd1963d2a 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -175,22 +175,29 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] if ( Match( tok, "[=,(] %var1% [,);]", varnames ) ) return; - // Return the memory.. - if ( Match( tok, "return %var1%", varnames ) ) - return; - // Return without deallocating the memory.. if ( Alloc != No && alloc_indentlevel >= 0 && Match(tok, "return") ) { + bool retvar = false; for ( const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next ) { if ( Match( tok2, "%var1%", varnames ) ) - return; - if ( tok2->str[0] == ';' ) + { + retvar = true; break; + } + + if ( tok2->str[0] == ';' ) + { + break; + } } - MemoryLeak( tok, varname ); - return; + + if ( ! retvar ) + MemoryLeak( tok, varname ); + + if ( indentlevel <= alloc_indentlevel ) + return; } } } diff --git a/tests.cpp b/tests.cpp index b0548a9f4..324da9d55 100644 --- a/tests.cpp +++ b/tests.cpp @@ -617,6 +617,17 @@ static void memleak_in_function() "}\n"; 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" ); } //---------------------------------------------------------------------------