From 8d079f2ee23cb4afda081af830bafe2079d93480 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 22 Feb 2024 10:32:34 +0100 Subject: [PATCH] Fix #12313 FP mallocOnClassWarning when malloc'd pointer is passed to implicit conversion (#6019) --- lib/checkclass.cpp | 2 +- test/testclass.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 08e75aef738..7e4d4adc443 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1429,7 +1429,7 @@ void CheckClass::checkMemset() const std::set parsedTypes; checkMemsetType(scope, tok, type, false, parsedTypes); } - } else if (tok->variable() && tok->variable()->typeScope() && Token::Match(tok, "%var% = %name% (")) { + } else if (tok->variable() && tok->variable()->isPointer() && tok->variable()->typeScope() && Token::Match(tok, "%var% = %name% (")) { const Library::AllocFunc* alloc = mSettings->library.getAllocFuncInfo(tok->tokAt(2)); if (!alloc) alloc = mSettings->library.getReallocFuncInfo(tok->tokAt(2)); diff --git a/test/testclass.cpp b/test/testclass.cpp index 6c26bb24add..5938170a1f5 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -3598,6 +3598,15 @@ class TestClass : public TestFixture { " AutoCloseFD fd = open(\"abc\", O_RDONLY | O_CLOEXEC);\n" "}"); ASSERT_EQUALS("", errout.str()); + + checkNoMemset("struct C {\n" // #12313 + " char* p;\n" + " C(char* ptr) : p(ptr) {}\n" + "};\n" + "void f() {\n" + " C c = strdup(\"abc\");\n" + "}"); + ASSERT_EQUALS("", errout.str()); } #define checkThisSubtraction(code) checkThisSubtraction_(code, __FILE__, __LINE__)