From 9bac4aca755c53af8933749dac9a5412fcb48b04 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Sun, 7 Jun 2009 09:55:20 +0300 Subject: [PATCH] Fix ticket #371 (Resource leak when exit() and if() uses together) http://apps.sourceforge.net/trac/cppcheck/ticket/371 --- src/checkmemoryleak.cpp | 21 +++++++++++++++++---- test/testmemleak.cpp | 8 ++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) 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()