From 74c0ff5cd91d3231c9bdefe4e3cbe775eb6830e5 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas <8551443+dudantas@users.noreply.github.com> Date: Tue, 28 Mar 2023 17:58:25 -0300 Subject: [PATCH 1/3] fix: add error handling to date formatting functions Thi adds try/catch blocks to the formatDate and formatDateShort functions to handle potential errors when formatting dates. In case of an error, the functions will log an error message using the SPDLOG_ERROR macro and return an empty string. This change improves the robustness of the code and prevents potential crashes when passing invalid values to the functions. --- src/utils/tools.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/utils/tools.cpp b/src/utils/tools.cpp index c040a83f77c..ab80732974a 100644 --- a/src/utils/tools.cpp +++ b/src/utils/tools.cpp @@ -347,11 +347,21 @@ std::string convertIPToString(uint32_t ip) { } std::string formatDate(time_t time) { - return fmt::format("{:%d/%m/%Y %H:%M:%S}", fmt::localtime(time)); + try { + return fmt::format("{:%d/%m/%Y %H:%M:%S}", fmt::localtime(time)); + } catch (std::exception const& exception) { + SPDLOG_ERROR("Failed to format date with error code {}", exception.what()); + } + return {}; } std::string formatDateShort(time_t time) { - return fmt::format("{:%Y-%m-%d %X}", fmt::localtime(time)); + try { + return fmt::format("{:%Y-%m-%d %X}", fmt::localtime(time)); + } catch (std::exception const& exception) { + SPDLOG_ERROR("Failed to format date short with error code {}", exception.what()); + } + return {}; } std::time_t getTimeNow() { From dc7b6abde85f3c211034a07a4fd27351e1458c81 Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Tue, 28 Mar 2023 20:59:02 +0000 Subject: [PATCH 2/3] Code format - (Clang-format) --- src/utils/tools.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/tools.cpp b/src/utils/tools.cpp index ab80732974a..72be556a594 100644 --- a/src/utils/tools.cpp +++ b/src/utils/tools.cpp @@ -349,7 +349,7 @@ std::string convertIPToString(uint32_t ip) { std::string formatDate(time_t time) { try { return fmt::format("{:%d/%m/%Y %H:%M:%S}", fmt::localtime(time)); - } catch (std::exception const& exception) { + } catch (const std::exception &exception) { SPDLOG_ERROR("Failed to format date with error code {}", exception.what()); } return {}; @@ -358,7 +358,7 @@ std::string formatDate(time_t time) { std::string formatDateShort(time_t time) { try { return fmt::format("{:%Y-%m-%d %X}", fmt::localtime(time)); - } catch (std::exception const& exception) { + } catch (const std::exception &exception) { SPDLOG_ERROR("Failed to format date short with error code {}", exception.what()); } return {}; From 02a2b390cf4bd89bb23d943352f4b6acd0f6420b Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Fri, 31 Mar 2023 20:08:36 -0700 Subject: [PATCH 3/3] fix: more specific exception --- src/utils/tools.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/tools.cpp b/src/utils/tools.cpp index 72be556a594..2cf67eb3285 100644 --- a/src/utils/tools.cpp +++ b/src/utils/tools.cpp @@ -349,7 +349,7 @@ std::string convertIPToString(uint32_t ip) { std::string formatDate(time_t time) { try { return fmt::format("{:%d/%m/%Y %H:%M:%S}", fmt::localtime(time)); - } catch (const std::exception &exception) { + } catch (const std::out_of_range &exception) { SPDLOG_ERROR("Failed to format date with error code {}", exception.what()); } return {}; @@ -358,7 +358,7 @@ std::string formatDate(time_t time) { std::string formatDateShort(time_t time) { try { return fmt::format("{:%Y-%m-%d %X}", fmt::localtime(time)); - } catch (const std::exception &exception) { + } catch (const std::out_of_range &exception) { SPDLOG_ERROR("Failed to format date short with error code {}", exception.what()); } return {};