From 90c28220319ef38059e8137de7fa89ea5bb35be6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 12 Apr 2008 11:33:48 +0000 Subject: [PATCH] CheckMemoryLeak: fixed bug that caused false positives --- CheckMemoryLeak.cpp | 10 ++++++++++ tests.cpp | 9 +++++++++ 2 files changed, 19 insertions(+) diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index d417407e7..6ab3bc10f 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -174,11 +174,21 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] // foo( var1 ); 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") ) { + for ( const TOKEN *tok2 = tok->next; tok2; tok2 = tok2->next ) + { + if ( Match( tok2, "%var1%", varnames ) ) + return; + if ( tok2->str[0] == ';' ) + break; + } MemoryLeak( tok, varname ); return; } diff --git a/tests.cpp b/tests.cpp index 8c273f466..b0548a9f4 100644 --- a/tests.cpp +++ b/tests.cpp @@ -608,6 +608,15 @@ static void memleak_in_function() check( CheckMemoryLeak, __LINE__, test14, "[test.cpp:9]: Memory leak: f.str\n" ); */ + + + const char test15[] = "static char *f()\n" + "{\n" + " char *s = new char[100];\n" + " return (char *)s;\n" + "}\n"; + check( CheckMemoryLeak, __LINE__, test15, "" ); + } //---------------------------------------------------------------------------