From 1dd2ec4757d7c5ce97b3552d1a9b6772c24686f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 29 Aug 2009 16:55:43 +0200 Subject: [PATCH] Fixed #414 (memory leak in if-else construct not detected) --- src/checkmemoryleak.cpp | 24 ++++++++++++++++++++++++ test/testmemleak.cpp | 10 +++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index db8789593..4af3466a3 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -1270,6 +1270,30 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) done = false; } + // Reduce "} else .." => "} if .." + if (Token::simpleMatch(tok2, "} else")) + { + int indentlevel = 0; + for (const Token *tok3 = tok2; tok3; tok3 = tok3->previous()) + { + if (tok3->str() == "{") + { + --indentlevel; + if (indentlevel == 0) + { + if (tok3->previous()->str() == "if") + tok2->next()->str("if"); + } + if (indentlevel <= 0) + break; + } + else if (tok3->str() == "}") + { + ++indentlevel; + } + } + } + // Remove "catch ;" if (Token::simpleMatch(tok2->next(), "catch ;")) { diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index b9f8e172d..6da611d44 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -444,11 +444,15 @@ private: ASSERT_EQUALS("; ifv xxx ;", simplifycode("; ifv ; else xxx ;")); { - const char code[] = "; alloc ; if { dealloc ; return ; }"; - ASSERT_EQUALS(code, simplifycode(code)); - ASSERT_EQUALS("; alloc ;", simplifycode(code, true)); + const char code1[] = "; alloc ; if { dealloc ; return ; }"; + ASSERT_EQUALS(code1, simplifycode(code1)); + ASSERT_EQUALS("; alloc ;", simplifycode(code1, true)); + + const char code2[] = "; alloc ; if { dealloc ; return ; } else { return ; }"; + ASSERT_EQUALS("; alloc ; if return ;", simplifycode(code2)); } + // switch.. ASSERT_EQUALS("; alloc ; dealloc ;", simplifycode(";alloc;switch{case;break;};dealloc;"));