From 482a2f9d957a08b1bfeeaa1125c0aee0064437be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 26 Sep 2009 07:19:22 +0200 Subject: [PATCH] Fixed #718 (False positive, resource leak when break in a loop before exit) --- src/checkmemoryleak.cpp | 15 +++++++++++++++ test/testmemleak.cpp | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/checkmemoryleak.cpp b/src/checkmemoryleak.cpp index b4a59a1e9..3428bae0f 100644 --- a/src/checkmemoryleak.cpp +++ b/src/checkmemoryleak.cpp @@ -1152,6 +1152,14 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) done = false; } + // Reduce "if X ; else X ;" => "X ;" + else if (Token::Match(tok2->next(), "if %var% ; else %var% ;") && + std::string(tok2->strAt(2)) == std::string(tok2->strAt(5))) + { + Token::eraseTokens(tok2, tok2->tokAt(5)); + done = false; + } + // Two "if alloc ;" after one another.. perhaps only one of them can be executed each time else if (!_settings->_showAll && Token::Match(tok2, "[;{}] if alloc ; if alloc ;")) { @@ -1373,6 +1381,13 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) done = false; } + // Reduce "loop break ; => ";" + if (Token::Match(tok2->next(), "loop break|continue ;")) + { + Token::eraseTokens(tok2, tok2->tokAt(3)); + done = false; + } + // Reduce "loop if break ; => ";" if (Token::Match(tok2->next(), "loop if break|continue ; !!else")) { diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 6f86770f8..b06f653fc 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -460,11 +460,10 @@ private: ASSERT_EQUALS("; alloc ; dealloc ;", simplifycode("; alloc; if(!var) { return; } dealloc;")); ASSERT_EQUALS("; alloc ;", simplifycode("; if { alloc; } else { return; }")); ASSERT_EQUALS("; alloc ; dealloc ;", simplifycode("; alloc ; if(!var) { alloc ; } dealloc ;")); - ASSERT_EQUALS("; use ;", simplifycode("; if(var) use ;")); + ASSERT_EQUALS("; break ;", simplifycode("; if break ; else break ;")); ASSERT_EQUALS("; alloc ; dealloc ;\n; alloc ;", simplifycode("; alloc ; if(!var) { return ; } if { dealloc ; }")); - ASSERT_EQUALS("; if alloc ; else assign ; return use ;", simplifycode("; callfunc ; if callfunc { alloc ; } else { assign ; } return use ;")); // "if ; .." @@ -480,6 +479,7 @@ private: ASSERT_EQUALS("; alloc ; dealloc ;", simplifycode(";alloc;switch{case;break;};dealloc;")); // loops.. + ASSERT_EQUALS(";", simplifycode("; loop { break; }")); ASSERT_EQUALS(";", simplifycode("; loop { if { break; } }")); ASSERT_EQUALS("; loop alloc ;", simplifycode("; loop { alloc ; }")); ASSERT_EQUALS("; alloc ; alloc ;", simplifycode("; alloc ; do { alloc ; } loop ;"));