From de71095e82f13b4a94457fc772411bed17c0c7f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 25 Oct 2008 16:47:51 +0000 Subject: [PATCH] CheckMemoryLeak: Fixed bug 2190219 - False positive, Mismatching allocation and deallocation --- CheckMemoryLeak.cpp | 36 +++++++++++++++++++++++++++++++++++- testmemleak.cpp | 22 ++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index 6fa6eef88..295501f2c 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -198,6 +198,35 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[]) varnames[0] = varname; varnames[1] = 0; + + // If "--all" hasn't been given and there are a preprocessor command, bail out. + // TODO: Evaluate all possible configurations separately instead. + if ( ! ShowAll ) + { + int indentlevel = 0; + for ( const TOKEN *_tok = tok; _tok; _tok = _tok->next ) + { + if ( _tok->str[0] == '{' ) + { + indentlevel++; + } + + else if ( _tok->str[0] == '}' ) + { + if ( indentlevel <= 0 ) + break; + indentlevel--; + } + + else if ( _tok->str[0] == '#' ) + { + return NULL; + } + } + } + + + TOKEN *rethead = 0, *rettail = 0; #define addtoken(_str) \ { \ @@ -354,6 +383,7 @@ static TOKEN *getcode(const TOKEN *tok, const char varname[]) return rethead; } + static void erase(TOKEN *begin, const TOKEN *end) { if ( ! begin ) @@ -369,7 +399,11 @@ static void erase(TOKEN *begin, const TOKEN *end) } -// Simpler but less powerful than "CheckMemoryLeak_CheckScope_All" +/** + * Check the allocation and deallocation for a function variable + * \param Tok1 The token where the variable is declared + * \param varname The name of the variable + **/ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] ) { TOKEN *tok = getcode( Tok1, varname ); diff --git a/testmemleak.cpp b/testmemleak.cpp index fda0d082e..a8cca723c 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -57,6 +57,7 @@ private: TEST_CASE( switch2 ); TEST_CASE( mismatch1 ); + TEST_CASE( mismatch2 ); TEST_CASE( func1 ); TEST_CASE( func2 ); @@ -403,6 +404,27 @@ private: } + void mismatch2() + { + // This code is taken directly from bug 2190219 - False positive, Mismatching allocation and deallocation + check( "void foo()\n" + "{\n" + "#ifdef __cplusplus\n" + " int* flags = new int[10];\n" + "#else\n" + " int* flags = (int*)malloc((10)*sizeof(int));\n" + "#endif\n" + "\n" + "#ifdef __cplusplus\n" + " delete [] flags;\n" + "#else\n" + " free(flags);\n" + "#endif\n" + "}\n"); + ASSERT_EQUALS( std::string(""), errout.str() ); + } + +