From 54374a9c7b05ce14851ff10b32d0ea86f8e666a0 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Mon, 16 Feb 2026 22:46:31 +0100 Subject: [PATCH] Fix #14172 FN leakReturnValNotUsed with scope operator (regression) --- lib/checkmemoryleak.cpp | 5 ++++- test/testmemleak.cpp | 12 ++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index ac6329bcf86..542f4e9fc49 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -1098,7 +1098,10 @@ void CheckMemoryLeakNoVar::checkForUnusedReturnValue(const Scope *scope) if (allocType == No) continue; - if (tok != tok->next()->astOperand1() && !isNew) + const Token* ftok = tok->next()->astOperand1(); + while (Token::simpleMatch(ftok, "::")) + ftok = ftok->astOperand2() ? ftok->astOperand2() : ftok->astOperand1(); + if (tok != ftok && !isNew) continue; if (isReopenStandardStream(tok)) diff --git a/test/testmemleak.cpp b/test/testmemleak.cpp index 406f46de902..26877650584 100644 --- a/test/testmemleak.cpp +++ b/test/testmemleak.cpp @@ -2752,6 +2752,18 @@ class TestMemleakNoVar : public TestFixture { " if (std::freopen(NULL, \"w+b\", fp2) == NULL) {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + check("void f() {\n" // #14172 + " if (malloc(4) == nullptr) {}\n" + " if (::malloc(4) == nullptr) {}\n" + " if (std::malloc(4) == nullptr) {}\n" + " if (::std::malloc(4) == nullptr) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2:9]: (error) Return value of allocation function 'malloc' is not stored. [leakReturnValNotUsed]\n" + "[test.cpp:3:11]: (error) Return value of allocation function 'malloc' is not stored. [leakReturnValNotUsed]\n" + "[test.cpp:4:14]: (error) Return value of allocation function 'malloc' is not stored. [leakReturnValNotUsed]\n" + "[test.cpp:5:16]: (error) Return value of allocation function 'malloc' is not stored. [leakReturnValNotUsed]\n", + errout_str()); } void smartPointerFunctionParam() {