From 6a053e0366662b77c5216f11fa09acdfa80831dc Mon Sep 17 00:00:00 2001 From: "Fabio R. Sluzala" Date: Tue, 26 Aug 2025 14:25:01 -0300 Subject: [PATCH] Potential fix for code scanning alert no. 1: Use of potentially dangerous function Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> --- .../LastModified.hpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/PistacheCustomHttpHeaders/LastModified.hpp b/src/PistacheCustomHttpHeaders/LastModified.hpp index 23e31eb..cd109c3 100644 --- a/src/PistacheCustomHttpHeaders/LastModified.hpp +++ b/src/PistacheCustomHttpHeaders/LastModified.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace PistacheCustomHttpHeaders { class LastModified : public Pistache::Http::Header::Header { @@ -38,10 +39,24 @@ class LastModified : public Pistache::Http::Header::Header { static auto format(std::time_t cftime) -> std::string { std::array buffer{}; - + std::tm tm_time{}; +#if defined(_WIN32) || defined(_WIN64) + // Use gmtime_s on Windows + errno_t err = gmtime_s(&tm_time, &cftime); + if (err != 0) { + // Unable to convert time + return {}; + } +#else + // Use gmtime_r on POSIX + if (!gmtime_r(&cftime, &tm_time)) { + // Unable to convert time + return {}; + } +#endif size_t strft_res_sz = strftime(buffer.data(), buffer.size(), "%a, %d %b %Y %H:%M:%S GMT", - std::gmtime(&cftime)); + &tm_time); return {buffer.data(), strft_res_sz}; }