diff --git a/CheckMemoryLeak.cpp b/CheckMemoryLeak.cpp index 8386eb93f..918d2f8a3 100644 --- a/CheckMemoryLeak.cpp +++ b/CheckMemoryLeak.cpp @@ -517,14 +517,14 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] continue; } } - - // Delete "if dealloc ;" and "if use ;" that is not followed by an else.. - if ((Match(tok2, "[;{}] if dealloc ;") || Match(tok2, "[;{}] if use ;")) && - !Match(Tokenizer::gettok(tok2,4), "else")) - { - erase(tok2->next, Tokenizer::gettok(tok2,3)); - done = false; - } + + // Delete "if dealloc ;" and "if use ;" that is not followed by an else.. + if ((Match(tok2, "[;{}] if dealloc ;") || Match(tok2, "[;{}] if use ;")) && + !Match(Tokenizer::gettok(tok2,4), "else")) + { + erase(tok2->next, Tokenizer::gettok(tok2,3)); + done = false; + } // Delete if block: "alloc; if return use ;" if (Match(tok2,"alloc ; if return use ;") && !Match(Tokenizer::gettok(tok2,6),"else")) @@ -596,7 +596,7 @@ static void CheckMemoryLeak_CheckScope( const TOKEN *Tok1, const char varname[] } // Delete second use in "use ; use ;" - while (Match(tok2, "use ; use ;")) + while (Match(tok2, "[;{}] use ; use ;")) { erase(tok2, Tokenizer::gettok(tok2,3)); done = false; diff --git a/testmemleak.cpp b/testmemleak.cpp index 5ab71332e..e5f614efa 100644 --- a/testmemleak.cpp +++ b/testmemleak.cpp @@ -39,7 +39,7 @@ private: { // Tokenize.. tokens = tokens_back = NULL; - std::istringstream istr(code); + std::istringstream istr(code); Tokenizer tokenizer; tokenizer.TokenizeCode( istr ); tokenizer.SimplifyTokenList(); @@ -63,9 +63,10 @@ private: TEST_CASE( simple5 ); TEST_CASE( simple6 ); TEST_CASE( simple7 ); - TEST_CASE( simple8 ); - + TEST_CASE( simple8 ); + TEST_CASE( use1 ); + TEST_CASE( use2 ); TEST_CASE( ifelse1 ); TEST_CASE( ifelse2 ); @@ -193,21 +194,34 @@ private: - - void use1() - { - check( "void foo()\n" - "{\n" - " char *str = strdup(\"abc\");\n" - " if (somecondition)\n" - " DeleteString(str);\n" - "}\n" ); - - ASSERT_EQUALS( std::string("[test.cpp:6]: Memory leak: str\n"), errout.str() ); - } - - - + + void use1() + { + check( "void foo()\n" + "{\n" + " char *str = strdup(\"abc\");\n" + " if (somecondition)\n" + " DeleteString(str);\n" + "}\n" ); + + ASSERT_EQUALS( std::string("[test.cpp:6]: Memory leak: str\n"), errout.str() ); + } + + + void use2() + { + check( "void foo()\n" + "{\n" + " char *str = strdup(\"abc\");\n" + " if ( abc ) { memset(str, 0, 3); }\n" + " *somestr = str;\n" + "}\n" ); + + ASSERT_EQUALS( std::string(""), errout.str() ); + } + + +