From 3401f228269d43f64d54ea3cc9ce155c6e45afdf Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 13 Jul 2024 16:30:17 +0200 Subject: [PATCH] Fix #12890 FP mismatchAllocDealloc with pointer to pointer --- lib/checkmemoryleak.cpp | 6 ++++-- test/testleakautovar.cpp | 13 +++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/checkmemoryleak.cpp b/lib/checkmemoryleak.cpp index 76aea729a55..70e578b8d21 100644 --- a/lib/checkmemoryleak.cpp +++ b/lib/checkmemoryleak.cpp @@ -67,10 +67,12 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2, return No; if (tok2->str() == "::") tok2 = tok2->next(); + while (Token::Match(tok2, "%name% :: %type%")) + tok2 = tok2->tokAt(2); if (!tok2->isName()) return No; - if (!Token::Match(tok2, "%name% ::|. %type%")) { + if (!Token::Match(tok2, "%name% . %type%")) { // Using realloc.. AllocType reallocType = getReallocationType(tok2, varid); if (reallocType != No) @@ -124,7 +126,7 @@ CheckMemoryLeak::AllocType CheckMemoryLeak::getAllocationType(const Token *tok2, } } - while (Token::Match(tok2,"%name% ::|. %type%")) + while (Token::Match(tok2,"%name% . %type%")) tok2 = tok2->tokAt(2); // User function diff --git a/test/testleakautovar.cpp b/test/testleakautovar.cpp index 06266aa2dd8..154a506a01b 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -621,6 +621,19 @@ class TestLeakAutoVar : public TestFixture { " delete[] li.front().m_p;\n" "}\n", true); ASSERT_EQUALS("", errout_str()); + + check("struct S {n" // #12890 + " int** p;n" + " S() {n" + " p = std::malloc(sizeof(int*));n" + " p[0] = new int;n" + " }n" + " ~S() {n" + " delete p[0];n" + " std::free(p);n" + " }n" + "};\n", true); + ASSERT_EQUALS("", errout_str()); } void assign26() {