Memory leaks: handling asprintf better

This commit is contained in:
Daniel Marjamäki 2009-08-29 07:26:32 +02:00
parent 406fdd3219
commit de9e62e90a
2 changed files with 10 additions and 0 deletions

View File

@ -634,6 +634,12 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
if (Token::simpleMatch(tok, "asprintf ("))
{
// todo: check how the return value is used.
if (!Token::Match(tok->previous(), "[;{}]"))
{
Tokenizer::deleteTokens(rethead);
return 0;
}
alloc = Malloc;
tok = tok->next()->link();
}

View File

@ -355,6 +355,10 @@ private:
ASSERT_EQUALS(";;alloc;", getcode("char *s; asprintf(&s, \"s: %s\", s);", "s"));
ASSERT_EQUALS(";;;", getcode("char *s; asprintf(&p, \"s: %s\", s);", "s"));
// Since we don't check how the return value is used we must bail out
ASSERT_EQUALS("", getcode("char *s; int ret = asprintf(&s, \"xyz\");", "s"));
TODO_ASSERT_EQUALS(";;alloc;", getcode("char *s; int ret; ret=asprintf(&s, \"xyz\"); if (ret==-1) return;", "s"));
// use..
ASSERT_EQUALS(";;use;", getcode("char *s; DeleteString(s);", "s"));
ASSERT_EQUALS(";;use;", getcode("char *s; s2 = s;", "s"));