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() {