Fixed #2214 (Improve check: Memory leak not detected when pointer is incremented)

This commit is contained in:
Daniel Marjamäki 2010-11-20 08:35:23 +01:00
parent fd64a7e683
commit 9ccc57a455
2 changed files with 15 additions and 6 deletions

View File

@ -214,11 +214,12 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getDeallocationType(const Token *tok
if (Token::Match(tok, "delete [ ] ( %varid% ) ;", varid))
return NewArray;
if (Token::Match(tok, "free ( %varid% ) ;", varid) ||
Token::Match(tok, "kfree ( %varid% ) ;", varid))
if (Token::Match(tok, "free|kfree ( %varid% ) ;", varid) ||
Token::Match(tok, "free|kfree ( %varid% -", varid))
return Malloc;
if (Token::Match(tok, "g_free ( %varid% ) ;", varid))
if (Token::Match(tok, "g_free ( %varid% ) ;", varid) ||
Token::Match(tok, "g_free ( %varid% -", varid))
return gMalloc;
if (Token::Match(tok, "fclose ( %varid% )", varid) ||
@ -1016,19 +1017,24 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
{
// is the pointer in rhs?
bool rhs = false;
for (const Token *tok2 = tok; tok2; tok2 = tok2->next())
for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next())
{
if (tok2->str() == ";")
{
if (rhs)
tok = tok2;
break;
}
if (Token::Match(tok2, "[=+] %varid%", varid))
{
rhs = true;
break;
}
}
addtoken(&rettail, tok, (rhs ? "use" : "assign"));
if (!rhs)
addtoken(&rettail, tok, "assign");
continue;
}
}

View File

@ -534,6 +534,9 @@ private:
ASSERT_EQUALS(";;;;", getcode("char *p; const char *q; q = p;", "p"));
ASSERT_EQUALS(";;use;;", getcode("char *s; x = {1,s};", "s"));
// non-use..
ASSERT_EQUALS(";;", getcode("char *s; s = s + 1;", "s"));
// return..
ASSERT_EQUALS(";;return;", getcode("char *s; return;", "s"));
ASSERT_EQUALS(";;returnuse;", getcode("char *s; return s;", "s"));