Fixed #5993 (FP: memleak (linux list))

This commit is contained in:
Daniel Marjamäki 2014-11-07 07:44:12 +01:00
parent 7e0fc3d481
commit 8012ac9562
2 changed files with 25 additions and 2 deletions

View File

@ -1345,8 +1345,22 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
}
// Linux lists..
if (varid > 0 && Token::Match(tok, "[=(,] & %varid% [.[,)]", varid)) {
addtoken(&rettail, tok, "&use");
if (varid > 0 && Token::Match(tok, "[=(,] & (| %varid% [.[,)]", varid)) {
// Is variable passed to a "leak-ignore" function?
bool leakignore = false;
if (Token::Match(tok, "[(,]")) {
const Token *parent = tok;
while (parent && parent->str() != "(")
parent = parent->astParent();
if (parent && parent->astOperand1() && parent->astOperand1()->isName()) {
const std::string &functionName = parent->astOperand1()->str();
if (_settings->library.leakignore.find(functionName) != _settings->library.leakignore.end())
leakignore = true;
}
}
// Not passed to "leak-ignore" function, add "&use".
if (!leakignore)
addtoken(&rettail, tok, "&use");
}
}

View File

@ -258,6 +258,7 @@ private:
TEST_CASE(throw2);
TEST_CASE(linux_list_1);
TEST_CASE(linux_list_2);
TEST_CASE(sizeof1);
@ -2855,6 +2856,14 @@ private:
ASSERT_EQUALS("", errout.str());
}
void linux_list_2() { // #5993
check("void foo() {\n"
" struct AB *ab = malloc(sizeof(struct AB));\n"
" list_add_tail(&(ab->list));\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void sizeof1() {