diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index 40e70ce51..3822e0020 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -809,13 +809,26 @@ void CheckMemoryLeakClass::simplifycode(Token *tok, bool &all) continue; // Found an "exit".. try to remove everything before it - const Token *tokEnd = tok2->next(); - while (tok2->previous() && !Token::Match(tok2->previous(), "[{}]")) - tok2 = tok2->previous(); - if (tok2->previous()) + Token *tokEnd = tok2->next(); + int indentlevel = 0; + while (tok2->previous()) + { tok2 = tok2->previous(); + if (tok2->str() == "}") + { + indentlevel--; + } + else if (tok2->str() == "{") + { + if (indentlevel == 0) + break; + + indentlevel++; + } + } Token::eraseTokens(tok2, tokEnd); + tok2 = tokEnd; } // reduce the code.. diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index cb14b8ad3..2b3ac0f3f 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2234,6 +2234,14 @@ private: " exit(0);\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("void f()\n" + "{\n" + " char *out = new char[100];\n" + " if( out ) {}\n" + " exit(0);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void stdstring()