Fixed #2113 (False positive: Memory leak where a pointer is passed in a struct to another function to be freed)

This commit is contained in:
Daniel Marjamäki 2010-10-20 18:39:40 +02:00
parent 02ba2b202e
commit 68dd2f8378
2 changed files with 31 additions and 0 deletions

View File

@ -1311,6 +1311,36 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
// Assignment..
if (varid)
{
if (Token::Match(tok, "= {"))
{
unsigned int indentlevel2 = 0;
bool use = false;
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
{
if (tok2->str() == "{")
++indentlevel2;
else if (tok2->str() == "}")
{
if (indentlevel2 <= 1)
break;
--indentlevel2;
}
else if (tok2->varId() == varid)
{
use = true;
break;
}
}
if (use)
{
addtoken(&rettail, tok, "use");
addtoken(&rettail, tok, ";");
tok = tok->next()->link();
continue;
}
}
if (Token::Match(tok, "[)=] %varid% [+;)]", varid) ||
Token::Match(tok, "%var% + %varid%", varid) ||
Token::Match(tok, "%varid% +=|-=", varid) ||

View File

@ -530,6 +530,7 @@ private:
ASSERT_EQUALS(";;use;if{;}", getcode("char *s; if (foo(s)) ;", "s"));
ASSERT_EQUALS(";;use;", getcode("char *s; map1[s] = 0;", "s"));
ASSERT_EQUALS(";;;;", getcode("char *p; const char *q; q = p;", "p"));
ASSERT_EQUALS(";;use;;", getcode("char *s; x = {1,s};", "s"));
// return..
ASSERT_EQUALS(";;return;", getcode("char *s; return;", "s"));