From 5e6fcbc60b378cb2901ebf24b024bce6978b6c96 Mon Sep 17 00:00:00 2001 From: Eduardo Arias Date: Mon, 12 Aug 2024 07:58:06 -0700 Subject: [PATCH] Replace usage of std::ctime, which is not safe for use in multithreaded contexts - std::ctime returns a pointer to a string that "may be shared between std::asctime and std::ctime, and may be overwritten on each invocation of any of those functions.". - https://en.cppreference.com/w/cpp/chrono/c/ctime - Replaced with call to strftime to generate the same string representation (using the format string: %c) - Leveraged localtime_r (which is thread-safe) to convert time_t to struct tm, as required by strftime. --- src/utils/string.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/utils/string.h b/src/utils/string.h index 700f33b9..b0c3c17d 100644 --- a/src/utils/string.h +++ b/src/utils/string.h @@ -21,6 +21,11 @@ #include #include #include +#include + +#ifdef WIN32 +#include "src/compat/msvc.h" +#endif #ifndef SRC_UTILS_STRING_H_ #define SRC_UTILS_STRING_H_ @@ -60,9 +65,11 @@ const char HEX2DEC[256] = { inline std::string ascTime(const time_t *t) { - std::string ts = std::ctime(t); - ts.pop_back(); - return ts; + struct tm timeinfo; + localtime_r(t, &timeinfo); + char tstr[std::size("Www Mmm dd hh:mm:ss yyyy")]; + strftime(tstr, std::size(tstr), "%c", &timeinfo); + return tstr; }