Skip to content

Commit 8012ac9

Browse files
committed
Fixed danmar#5993 (FP: memleak (linux list))
1 parent 7e0fc3d commit 8012ac9

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

lib/checkmemoryleak.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,8 +1345,22 @@ Token *CheckMemoryLeakInFunction::getcode(const Token *tok, std::list<const Toke
13451345
}
13461346

13471347
// Linux lists..
1348-
if (varid > 0 && Token::Match(tok, "[=(,] & %varid% [.[,)]", varid)) {
1349-
addtoken(&rettail, tok, "&use");
1348+
if (varid > 0 && Token::Match(tok, "[=(,] & (| %varid% [.[,)]", varid)) {
1349+
// Is variable passed to a "leak-ignore" function?
1350+
bool leakignore = false;
1351+
if (Token::Match(tok, "[(,]")) {
1352+
const Token *parent = tok;
1353+
while (parent && parent->str() != "(")
1354+
parent = parent->astParent();
1355+
if (parent && parent->astOperand1() && parent->astOperand1()->isName()) {
1356+
const std::string &functionName = parent->astOperand1()->str();
1357+
if (_settings->library.leakignore.find(functionName) != _settings->library.leakignore.end())
1358+
leakignore = true;
1359+
}
1360+
}
1361+
// Not passed to "leak-ignore" function, add "&use".
1362+
if (!leakignore)
1363+
addtoken(&rettail, tok, "&use");
13501364
}
13511365
}
13521366

test/testmemleak.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ class TestMemleakInFunction : public TestFixture {
258258
TEST_CASE(throw2);
259259

260260
TEST_CASE(linux_list_1);
261+
TEST_CASE(linux_list_2);
261262

262263
TEST_CASE(sizeof1);
263264

@@ -2855,6 +2856,14 @@ class TestMemleakInFunction : public TestFixture {
28552856
ASSERT_EQUALS("", errout.str());
28562857
}
28572858

2859+
void linux_list_2() { // #5993
2860+
check("void foo() {\n"
2861+
" struct AB *ab = malloc(sizeof(struct AB));\n"
2862+
" list_add_tail(&(ab->list));\n"
2863+
"}");
2864+
ASSERT_EQUALS("", errout.str());
2865+
}
2866+
28582867

28592868

28602869
void sizeof1() {

0 commit comments

Comments
 (0)