From: Otto Moerbeek Date: Tue, 17 May 2022 12:37:03 +0000 (+0200) Subject: Tsan complained about strftime(), it manulipates locale and/or tz info. X-Git-Tag: auth-4.8.0-alpha0~95^2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=68b1e63202aad0a6b5a74c04d0211312c4e1dc8a;p=thirdparty%2Fpdns.git Tsan complained about strftime(), it manulipates locale and/or tz info. So guard if needed. --- diff --git a/pdns/recursordist/rec-main.cc b/pdns/recursordist/rec-main.cc index b00a0af1fc..be46da934b 100644 --- a/pdns/recursordist/rec-main.cc +++ b/pdns/recursordist/rec-main.cc @@ -827,8 +827,14 @@ static std::string s_timestampFormat = "%s"; static const char* toTimestampStringMilli(const struct timeval& tv, char* buf, size_t sz) { - struct tm tm; - size_t len = strftime(buf, sz, s_timestampFormat.c_str(), localtime_r(&tv.tv_sec, &tm)); + size_t len = 0; + if (s_timestampFormat != "%s") { + // strftime is not thread safe, it can access locale information + static std::mutex m; + auto lock = std::lock_guard(m); + struct tm tm; + len = strftime(buf, sz, s_timestampFormat.c_str(), localtime_r(&tv.tv_sec, &tm)); + } if (len == 0) { len = snprintf(buf, sz, "%lld", static_cast(tv.tv_sec)); }