Fixed #1082 (False positive: Resource leak in loop which exits only after releasing resource)

This commit is contained in:
Daniel Marjamäki 2009-12-13 19:50:49 +01:00
parent 829a4f8453
commit 4dc8794c69
2 changed files with 10 additions and 6 deletions

View File

@ -1226,7 +1226,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
// Delete extra ";" // Delete extra ";"
while (Token::Match(tok2, "[;{}] ;")) while (Token::Match(tok2, "[;{}] ;"))
{ {
Token::eraseTokens(tok2, tok2->tokAt(2)); tok2->deleteNext();
done = false; done = false;
} }
@ -1241,13 +1241,13 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
// Delete braces around a single instruction.. // Delete braces around a single instruction..
if (Token::Match(tok2->next(), "{ %var% ; }")) if (Token::Match(tok2->next(), "{ %var% ; }"))
{ {
Token::eraseTokens(tok2, tok2->tokAt(2)); tok2->deleteNext();
Token::eraseTokens(tok2->tokAt(2), tok2->tokAt(4)); Token::eraseTokens(tok2->tokAt(2), tok2->tokAt(4));
done = false; done = false;
} }
if (Token::Match(tok2->next(), "{ %var% %var% ; }")) if (Token::Match(tok2->next(), "{ %var% %var% ; }"))
{ {
Token::eraseTokens(tok2, tok2->tokAt(2)); tok2->deleteNext();
Token::eraseTokens(tok2->tokAt(3), tok2->tokAt(5)); Token::eraseTokens(tok2->tokAt(3), tok2->tokAt(5));
done = false; done = false;
} }
@ -1271,7 +1271,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
// Delete empty if that is not followed by an else // Delete empty if that is not followed by an else
if (Token::Match(tok2->next(), "if ; !!else")) if (Token::Match(tok2->next(), "if ; !!else"))
{ {
Token::eraseTokens(tok2, tok2->tokAt(2)); tok2->deleteNext();
done = false; done = false;
} }
@ -1317,7 +1317,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
} }
else else
{ {
Token::eraseTokens(tok2, tok2->tokAt(2)); tok2->deleteNext();
} }
done = false; done = false;
} }
@ -1332,7 +1332,7 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
// "[;{}] if alloc ; else return ;" => "[;{}] alloc ;" // "[;{}] if alloc ; else return ;" => "[;{}] alloc ;"
else if (Token::Match(tok2, "[;{}] if alloc ; else return ;")) 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" Token::eraseTokens(tok2->next(), tok2->tokAt(5)); // Remove "; else return"
done = false; done = false;
} }
@ -1362,6 +1362,8 @@ void CheckMemoryLeakInFunction::simplifycode(Token *tok, bool &all)
else if (Token::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; }")) else if (Token::Match(tok2, "[;{}] if { dealloc|assign|use ; return ; }"))
{ {
Token::eraseTokens(tok2, tok2->tokAt(8)); Token::eraseTokens(tok2, tok2->tokAt(8));
if (Token::simpleMatch(tok2->next(), "else"))
tok2->deleteNext();
done = false; done = false;
} }

View File

@ -486,6 +486,8 @@ private:
ASSERT_EQUALS("; alloc ; dealloc ;\n; alloc ;", simplifycode("; alloc ; if(!var) { return ; } if { dealloc ; }")); 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("; 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 ; .." // "if ; .."
ASSERT_EQUALS("; if xxx ;", simplifycode("; if ; else xxx ;")); ASSERT_EQUALS("; if xxx ;", simplifycode("; if ; else xxx ;"));
ASSERT_EQUALS("; if(var) xxx ;", simplifycode("; if(!var) ; else xxx ;")); ASSERT_EQUALS("; if(var) xxx ;", simplifycode("; if(!var) ; else xxx ;"));