memory leaks: better handling of open/close

This commit is contained in:
Daniel Marjamäki 2009-08-29 09:18:21 +02:00
parent 88d0bd3908
commit 6347d0e976
2 changed files with 22 additions and 12 deletions

View File

@ -764,26 +764,31 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
// if else switch
if (tok->str() == "if")
{
if (alloctype == Fd &&
(Token::Match(tok, "if ( %varid% >= 0 )", varid) ||
Token::Match(tok, "if ( %varid% != -1 )", varid)))
if (alloctype == Fd)
{
addtoken("if(var)");
tok = tok->next()->link();
if (Token::Match(tok, "if ( %varid% >= 0 )", varid) ||
Token::Match(tok, "if ( %varid% != -1 )", varid))
{
addtoken("if(var)");
tok = tok->next()->link();
continue;
}
else if (Token::Match(tok, "if ( %varid% == -1 )", varid) ||
Token::Match(tok, "if ( %varid% < 0 )", varid))
{
addtoken("if(!var)");
tok = tok->next()->link();
continue;
}
}
else if (Token::Match(tok, "if ( %varid% )", varid))
if (Token::Match(tok, "if ( %varid% )", varid))
{
addtoken("if(var)");
// Make sure the "use" will not be added
tok = tok->next()->link();
}
else if (Token::Match(tok, "if ( %varid% == -1 )", varid) ||
Token::Match(tok, "if ( %varid% < 0 )", varid))
{
// FIXME: ensure then this variable has int type and uses as file descriptor
addtoken("if(!var)");
}
else if (Token::simpleMatch(tok, "if (") && notvar(tok->tokAt(2), varid, true))
{
addtoken("if(!var)");
@ -815,6 +820,10 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
dep = true;
break;
}
if (alloctype == Fd && Token::Match(tok2, "%varid% !=|>=", varid))
{
dep = true;
}
}
if (Token::Match(tok, "if ( ! %varid% &&", varid))

View File

@ -377,6 +377,7 @@ private:
// open/close
ASSERT_EQUALS(";;alloc;if(var)dealloc;", getcode("int f; f=open(); if(f>=0)close(f);", "f"));
ASSERT_EQUALS(";;alloc;ifv;", getcode("int f; f=open(); if(f!=-1 || x);", "f"));
}