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" ); } //---------------------------------------------------------------------------