From 85fcd4c9a93a40a1b7e8cc876f1cb56497b9264a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 19 Jan 2010 19:08:03 +0100 Subject: [PATCH] Fixed #1282 (False positive for a memory leak in a simple loop) --- lib/checkmemoryleak.cpp | 7 +++++++ test/testmemleak.cpp | 8 ++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 4bc3bc799..6270aa5e9 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1448,6 +1448,13 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) continue; } + // Reduce "alloc loop !var alloc ;" => "alloc ;" + if (Token::Match(tok2, "[;{}] alloc ; loop !var alloc ;")) + { + Token::eraseTokens(tok2, tok2->tokAt(5)); + done = false; + } + // Reduce "if(var) dealloc ;" and "if(var) use ;" that is not followed by an else.. if (Token::Match(tok2, "[;{}] if(var) assign|dealloc|use ; !!else")) { diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index e660c15d0..0fb93508b 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -555,6 +555,12 @@ private: Token::eraseTokens(tok, tok->tokAt(5)); tok->str("if(!var)"); } + + else if (Token::simpleMatch(tok, "! var")) + { + tok->deleteNext(); + tok->str("!var"); + } } Settings settings; @@ -635,6 +641,8 @@ private: ASSERT_EQUALS("; alloc ; alloc ;", simplifycode("; alloc ; do { alloc ; } loop ;")); ASSERT_EQUALS("; exit ;", simplifycode("; alloc ; do { } loop ; exit ;")); + ASSERT_EQUALS("; alloc ;", simplifycode("; alloc ; loop !var alloc ;")); + ASSERT_EQUALS("; alloc ; dealloc ; return ;", simplifycode("; alloc ; while1 { if { dealloc ; return ; } }")); ASSERT_EQUALS("; alloc ; dealloc ; return ;", simplifycode("; alloc ; while1 { if { dealloc ; return ; } if { continue ; } }")); ASSERT_EQUALS("; alloc ;", simplifycode("; alloc ; while1 { if { dealloc ; return ; } if { break ; } }"));