From 80f00797e3cb7a7ba8adfe605d50f108c3cd3ee4 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 17 Jan 2025 18:35:34 +0200 Subject: [PATCH] Fixed copy ctor of err_helper to be thread safe --- include/spdlog/details/err_helper.h | 3 ++- src/details/err_helper.cpp | 15 +++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/include/spdlog/details/err_helper.h b/include/spdlog/details/err_helper.h index a977b1ab..887948ef 100644 --- a/include/spdlog/details/err_helper.h +++ b/include/spdlog/details/err_helper.h @@ -15,9 +15,10 @@ namespace details { class SPDLOG_API err_helper { err_handler custom_err_handler_; std::chrono::steady_clock::time_point last_report_time_; - std::mutex mutex_; + mutable std::mutex mutex_; public: err_helper() = default; + ~err_helper() = default; err_helper(const err_helper& other); err_helper(err_helper&& other) noexcept; void handle_ex(const std::string& origin, const source_loc& loc, const std::exception& ex) noexcept; diff --git a/src/details/err_helper.cpp b/src/details/err_helper.cpp index d4441aa8..8b867098 100644 --- a/src/details/err_helper.cpp +++ b/src/details/err_helper.cpp @@ -8,13 +8,16 @@ namespace spdlog { namespace details { -err_helper::err_helper(const err_helper &other) - : custom_err_handler_(other.custom_err_handler_), - last_report_time_(other.last_report_time_) {} +err_helper::err_helper(const err_helper &other) { + std::lock_guard lock(other.mutex_); + custom_err_handler_ = other.custom_err_handler_; + last_report_time_ = other.last_report_time_; +} -err_helper::err_helper(err_helper &&other) noexcept - : custom_err_handler_(std::move(other.custom_err_handler_)), - last_report_time_(other.last_report_time_) {} +err_helper::err_helper(err_helper &&other) noexcept { + custom_err_handler_ = std::move(other.custom_err_handler_); + last_report_time_ = std::move(other.last_report_time_); +} // Prints error to stderr with source location (if available). A stderr sink is not used because reaching // this point might indicate a problem with the logging system itself so we use fputs() directly.