From 3cb392dbc3f1dac8974cbc0217f64043780a8549 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 17 Jul 2024 18:13:54 +0200 Subject: [PATCH] Fix #12890 FP mismatchAllocDealloc with pointer to pointer (#6598) --- lib/checkmemoryleak.cpp | 6 ++++-- test/testleakautovar.cpp | 42 ++++++++++++++++------------------------ 2 files changed, 21 insertions(+), 27 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 9e50ddfee9d..286c1a75c24 100644 --- a/test/testleakautovar.cpp +++ b/test/testleakautovar.cpp @@ -33,29 +33,7 @@ class TestLeakAutoVar : public TestFixture { Settings settings; void run() override { - constexpr char xmldata[] = "\n" - "\n" - " \n" - " \n" - " malloc\n" - " realloc\n" - " free\n" - " \n" - " \n" - " socket\n" - " close\n" - " \n" - " \n" - " fopen\n" - " freopen\n" - " fclose\n" - " \n" - " \n" - " \n" - " \n" - " \n" - ""; - settings = settingsBuilder(settings).libraryxml(xmldata, sizeof(xmldata)).build(); + settings = settingsBuilder(settings).library("std.cfg").build(); // Assign TEST_CASE(assign1); @@ -462,9 +440,10 @@ class TestLeakAutoVar : public TestFixture { } void assign22() { // #9139 + const Settings s = settingsBuilder().library("posix.cfg").build(); check("void f(char tempFileName[256]) {\n" " const int fd = socket(AF_INET, SOCK_PACKET, 0 );\n" - "}", true); + "}", true, &s); ASSERT_EQUALS("[test.cpp:3]: (error) Resource leak: fd\n", errout_str()); check("void f() {\n" @@ -622,6 +601,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() { @@ -652,7 +644,7 @@ class TestLeakAutoVar : public TestFixture { check("void f() {\n" " std::string *str = new std::string;" "}", true); - TODO_ASSERT_EQUALS("[test.cpp:2]: (error) Memory leak: str\n", "", errout_str()); + ASSERT_EQUALS("[test.cpp:2]: (error) Memory leak: str\n", errout_str()); check("class TestType {\n" // #9028 "public:\n"