From c2c66908f61ef3df8e6bffda0d348d447356d974 Mon Sep 17 00:00:00 2001 From: Julian Brost Date: Wed, 21 Aug 2024 11:43:13 +0200 Subject: [PATCH] Utility::FormatDateTime(): use localtime_s() on Windows localtime() is not thread-safe as it returns a pointer to a shared tm struct. Everywhere except on Windows, localtime_r() is used already which avoids the problem by using a struct allocated by the caller for the output. Windows actually has a similar function called localtime_s() which has the same properties, just with a different name and order of arguments. --- lib/base/utility.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index 6f272178a..6985ce539 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -1058,15 +1058,12 @@ String Utility::FormatDateTime(const char *format, double ts) tm tmthen; #ifdef _MSC_VER - tm *temp = localtime(&tempts); - - if (!temp) { + errno_t err = localtime_s(&tmthen, &tempts); + if (err) { BOOST_THROW_EXCEPTION(posix_error() - << boost::errinfo_api_function("localtime") - << boost::errinfo_errno(errno)); + << boost::errinfo_api_function("localtime_s") + << boost::errinfo_errno(err)); } - - tmthen = *temp; #else /* _MSC_VER */ if (!localtime_r(&tempts, &tmthen)) { BOOST_THROW_EXCEPTION(posix_error()