Skip to content

Commit 6e5b86b

Browse files
committed
clarified checks and fixed test
1 parent 4f3a25d commit 6e5b86b

2 files changed

Lines changed: 15 additions & 20 deletions

File tree

lib/checkleakautovar.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -279,36 +279,31 @@ static const Token * isFunctionCall(const Token * nameToken)
279279
* @param tok on the LHS of a function call
280280
* @return opening parenthesis token or nullptr if not a function call
281281
*/
282-
static const Token * isAnonymousFunctionCall(const Token * tok)
282+
static const Token *isAnonymousFunctionCall(const Token *tok)
283283
{
284-
// match one of the supported LHS patterns
285-
// TODO: check if tok->previous()->isCast(). can't right now because
286-
//
287-
// auto x = [](void *ptr) { g(ptr) };
288-
// void *p = malloc(1);
289-
// (x)(p);
290-
// ^
291-
// the lpar surrounding x has isCast() == true, so checking isCast() would
292-
// have false positive leaks, while allowing casts to take ownership of
293-
// resources is instead a false negative
294-
if (tok->strAt(-1) == "(" && !tok->previous()->isBinaryOp() &&
295-
tok->linkAt(-1) && !tok->isStandardType()) {
284+
if (!tok || tok->isStandardType())
285+
return nullptr;
286+
287+
auto isLparNotCast = [](const Token *lpar) -> bool {
288+
return !lpar->isCast() && lpar->str() == "(";
289+
};
290+
291+
// function pointer or lambda
292+
if (isLparNotCast(tok->previous())) {
296293
tok = tok->linkAt(-1)->next();
297-
} else if (!tok->isStandardType() && tok->isName() &&
298-
tok->strAt(1) == "(") {
294+
} else if (tok->isName() && isLparNotCast(tok->next())) {
295+
// call to result of a function
299296
tok = tok->linkAt(1)->next();
300297
} else {
301298
return nullptr;
302299
}
303300

304-
// skip over potential template arguments
301+
// < could be a less than, not a template operator
305302
if (tok->link() && tok->str() == "<")
306303
tok = tok->link()->next();
307304

308-
// return the opening parenthesis
309-
if (tok && tok->link() && !tok->isCast() && tok->str() == "(")
305+
if (isLparNotCast(tok))
310306
return tok;
311-
312307
return nullptr;
313308
}
314309

test/testleakautovar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1924,7 +1924,7 @@ class TestLeakAutoVar : public TestFixture {
19241924
"auto x = [](void *ptr) { g(ptr) };\n"
19251925
"void *p = malloc(1);\n"
19261926
"(x)(p);\n"
1927-
"}\n");
1927+
"}\n", dinit(CheckOptions, $.cpp = true));
19281928
ASSERT_EQUALS("", errout_str());
19291929

19301930
// Function returning a function pointer

0 commit comments

Comments
 (0)