From 5c59d8600423558ad05462bc4a5fca0f7f88ff2c Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 31 Jan 2024 20:57:46 +0100 Subject: [PATCH] Fix #12403 FP mallocOnClassWarning for RAII class (#5930) --- lib/checkclass.cpp | 8 ++++++-- test/testclass.cpp | 13 ++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 86431a902d2..666c18dfbb6 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -1429,8 +1429,12 @@ 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% (") && - (mSettings->library.getAllocFuncInfo(tok->tokAt(2)) || mSettings->library.getReallocFuncInfo(tok->tokAt(2)))) { + } else if (tok->variable() && 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)); + if (!alloc || alloc->bufferSize == Library::AllocFunc::BufferSize::none) + continue; const std::set parsedTypes; checkMemsetType(scope, tok->tokAt(2), tok->variable()->typeScope(), true, parsedTypes); diff --git a/test/testclass.cpp b/test/testclass.cpp index 5bf9597c40d..601f384ffcf 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -2955,7 +2955,7 @@ class TestClass : public TestFixture { #define checkNoMemset(...) checkNoMemset_(__FILE__, __LINE__, __VA_ARGS__) void checkNoMemset_(const char* file, int line, const char code[]) { - const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::portability).library("std.cfg").build(); + const Settings settings = settingsBuilder().severity(Severity::warning).severity(Severity::portability).library("std.cfg").library("posix.cfg").build(); checkNoMemset_(file, line, code, settings); } @@ -3587,6 +3587,17 @@ class TestClass : public TestFixture { " p = malloc(sizeof(C));\n" "}"); ASSERT_EQUALS("", errout.str()); + + checkNoMemset("class AutoCloseFD {\n" + " int fd;\n" + "public:\n" + " AutoCloseFD(int fd);\n" + " ~AutoCloseFD();\n" + "};\n" + "void f() {\n" + " AutoCloseFD fd = open(\"abc\", O_RDONLY | O_CLOEXEC);\n" + "}"); + ASSERT_EQUALS("", errout.str()); } #define checkThisSubtraction(code) checkThisSubtraction_(code, __FILE__, __LINE__)