From a0879d87443e13091d872defe071d8953ac3ebab Mon Sep 17 00:00:00 2001 From: qicosmos Date: Wed, 5 Feb 2025 11:28:19 +0800 Subject: [PATCH] [time][fix]call safe time function (#891) --- include/ylt/easylog/appender.hpp | 17 ++++++++++++++++- include/ylt/standalone/cinatra/time_util.hpp | 17 ++++++++++++++++- include/ylt/util/time_util.h | 17 ++++++++++++++++- src/coro_http/tests/test_cinatra_websocket.cpp | 2 +- 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/include/ylt/easylog/appender.hpp b/include/ylt/easylog/appender.hpp index 57ad1dc1c..4a6636283 100644 --- a/include/ylt/easylog/appender.hpp +++ b/include/ylt/easylog/appender.hpp @@ -47,6 +47,20 @@ inline void to_int(int num, char *p, int &size) { p[--size] = c; } +inline std::tm localtime_safe(std::time_t timer) { + std::tm bt{}; +#if defined(__unix__) + localtime_r(&timer, &bt); +#elif defined(_MSC_VER) + localtime_s(&bt, &timer); +#else + static std::mutex mtx; + std::lock_guard lock(mtx); + bt = *std::localtime(&timer); +#endif + return bt; +} + inline char *get_time_str(const auto &now) { static thread_local char buf[33]; static thread_local std::chrono::seconds last_sec_{}; @@ -63,7 +77,8 @@ inline char *get_time_str(const auto &now) { last_sec_ = s; auto tm = std::chrono::system_clock::to_time_t(now); - auto gmt = localtime(&tm); + auto ltm = localtime_safe(tm); + std::tm *gmt = <m; to_int<3, '.'>(mill_sec, buf, size); to_int<2, ':'>(gmt->tm_sec, buf, size); diff --git a/include/ylt/standalone/cinatra/time_util.hpp b/include/ylt/standalone/cinatra/time_util.hpp index 0ef7c29e6..a0eb3345e 100644 --- a/include/ylt/standalone/cinatra/time_util.hpp +++ b/include/ylt/standalone/cinatra/time_util.hpp @@ -278,11 +278,26 @@ inline void to_hour(char *buf, int day, char c) { to_int<2>(day, c, buf); } inline void to_min(char *buf, int day, char c) { to_int<2>(day, c, buf); } inline void to_sec(char *buf, int day, char c) { to_int<2>(day, c, buf); } +inline std::tm gmtime_safe(std::time_t &timer) { + std::tm bt{}; +#if defined(__unix__) + gmtime_r(&timer, &bt); +#elif defined(_MSC_VER) + gmtime_s(&bt, &timer); +#else + static std::mutex mtx; + std::lock_guard lock(mtx); + bt = *gmtime(&timer); +#endif + return bt; +} + template inline std::string_view get_local_time_str(char (&buf)[N], std::time_t t, std::string_view format) { static_assert(N >= 20, "wrong buf"); - struct tm *loc_time = gmtime(&t); + std::tm tm = gmtime_safe(t); + std::tm *loc_time = &tm; char *p = buf; diff --git a/include/ylt/util/time_util.h b/include/ylt/util/time_util.h index 0c0f3fb56..96716d087 100644 --- a/include/ylt/util/time_util.h +++ b/include/ylt/util/time_util.h @@ -424,11 +424,26 @@ inline void to_hour(char *buf, int day, char c) { to_int<2>(day, c, buf); } inline void to_min(char *buf, int day, char c) { to_int<2>(day, c, buf); } inline void to_sec(char *buf, int day, char c) { to_int<2>(day, c, buf); } +inline std::tm gmtime_safe(std::time_t &timer) { + std::tm bt{}; +#if defined(__unix__) + gmtime_r(&timer, &bt); +#elif defined(_MSC_VER) + gmtime_s(&bt, &timer); +#else + static std::mutex mtx; + std::lock_guard lock(mtx); + bt = *gmtime(&timer); +#endif + return bt; +} + template inline std::string_view get_local_time_str(char (&buf)[N], std::time_t t, std::string_view format) { static_assert(N >= 20, "wrong buf"); - struct tm *loc_time = gmtime(&t); + std::tm tm = gmtime_safe(t); + std::tm *loc_time = &tm; char *p = buf; diff --git a/src/coro_http/tests/test_cinatra_websocket.cpp b/src/coro_http/tests/test_cinatra_websocket.cpp index f57095fc7..21effcd1b 100644 --- a/src/coro_http/tests/test_cinatra_websocket.cpp +++ b/src/coro_http/tests/test_cinatra_websocket.cpp @@ -317,7 +317,7 @@ TEST_CASE("test read write in different threads") { CHECK(data.resp_body == send_str); } }; - another_thread_lazy().via(coro_io::get_global_executor()).start([](auto &&) { + another_thread_lazy().start([](auto &&) { }); auto lazy = [client, weak, &send_str]() -> async_simple::coro::Lazy {