Memory leaks: refactoring return handling

This commit is contained in:
Daniel Marjamäki 2010-08-04 23:01:32 +02:00
parent 239d264432
commit 9d24c9ceaa
2 changed files with 19 additions and 10 deletions

View File

@ -1264,12 +1264,6 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
} }
} }
else if (varid && Token::Match(tok, "return &| %varid%", varid))
{
if (!Token::Match(tok, "return %var% ["))
addtoken("use");
}
else if (varid && Token::Match(tok, "return strcpy|strncpy|memcpy ( %varid%", varid)) else if (varid && Token::Match(tok, "return strcpy|strncpy|memcpy ( %varid%", varid))
{ {
addtoken("use"); addtoken("use");
@ -1278,18 +1272,32 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
else else
{ {
bool use = false;
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
{ {
if (tok2->str() == ";") if (tok2->str() == ";")
break;
if (tok2->varId() == varid)
{ {
addtoken("use");
tok = tok2; tok = tok2;
break; break;
} }
if (tok2->varId() == varid)
{
// Read data..
if (!Token::Match(tok2->previous(), "&|(") &&
Token::simpleMatch(tok2->next(), "["))
{
}
else
{
use = true;
}
}
} }
if (use)
addtoken("use");
addtoken(";");
} }
} }

View File

@ -524,6 +524,7 @@ private:
// return.. // return..
ASSERT_EQUALS(";;return;", getcode("char *s; return;", "s")); ASSERT_EQUALS(";;return;", getcode("char *s; return;", "s"));
ASSERT_EQUALS(";;returnuse;", getcode("char *s; return s;", "s")); ASSERT_EQUALS(";;returnuse;", getcode("char *s; return s;", "s"));
ASSERT_EQUALS(";;return;", getcode("char *s; return 5 + s[0];", "s"));
// assign.. // assign..
ASSERT_EQUALS(";;assign;", getcode("char *s; s = 0;", "s")); ASSERT_EQUALS(";;assign;", getcode("char *s; s = 0;", "s"));