From 4dc8794c691ec2dd237487429f5140bbf41ebd16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 13 Dec 2009 19:50:49 +0100 Subject: [PATCH] Fixed #1082 (False positive: Resource leak in loop which exits only after releasing resource) --- lib/checkmemoryleak.cpp | 14 ++++++++------ test/testmemleak.cpp | 2 ++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index ed24a7f79..7f85fd8d2 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1226,7 +1226,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) // Delete extra ";" while (Token::Match(tok2, "[;{}] ;")) { - Token::eraseTokens(tok2, tok2->tokAt(2)); + tok2->deleteNext(); done = false; } @@ -1241,13 +1241,13 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) // Delete braces around a single instruction.. if (Token::Match(tok2->next(), "{ %var% ; }")) { - Token::eraseTokens(tok2, tok2->tokAt(2)); + tok2->deleteNext(); Token::eraseTokens(tok2->tokAt(2), tok2->tokAt(4)); done = false; } if (Token::Match(tok2->next(), "{ %var% %var% ; }")) { - Token::eraseTokens(tok2, tok2->tokAt(2)); + tok2->deleteNext(); Token::eraseTokens(tok2->tokAt(3), tok2->tokAt(5)); done = false; } @@ -1271,7 +1271,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) // Delete empty if that is not followed by an else if (Token::Match(tok2->next(), "if ; !!else")) { - Token::eraseTokens(tok2, tok2->tokAt(2)); + tok2->deleteNext(); done = false; } @@ -1317,7 +1317,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) } else { - Token::eraseTokens(tok2, tok2->tokAt(2)); + tok2->deleteNext(); } done = false; } @@ -1332,7 +1332,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) // "[;{}] if alloc ; else return ;" => "[;{}] alloc ;" else if (Token::Match(tok2, "[;{}] if alloc ; else return ;")) { - Token::eraseTokens(tok2, tok2->tokAt(2)); // Remove "if" + tok2->deleteNext(); // Remove "if" Token::eraseTokens(tok2->next(), tok2->tokAt(5)); // Remove "; else return" done = false; } @@ -1362,6 +1362,8 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all) else if (Token::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; }")) { Token::eraseTokens(tok2, tok2->tokAt(8)); + if (Token::simpleMatch(tok2->next(), "else")) + tok2->deleteNext(); done = false; } diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index dd87a4c73..a7df4468c 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -486,6 +486,8 @@ private: 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 ;")); + ASSERT_EQUALS("; while1 continue ;", simplifycode("; while1 { if callfunc { dealloc ; return ; } else { continue ; } }")); + // "if ; .." ASSERT_EQUALS("; if xxx ;", simplifycode("; if ; else xxx ;")); ASSERT_EQUALS("; if(var) xxx ;", simplifycode("; if(!var) ; else xxx ;"));