From 3b2fd85b59c54380b7acfc0297766907beb2ed9f Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Thu, 1 Dec 2016 11:47:39 +0100 Subject: [PATCH] Merge #9230: Fix some benign races in timestamp logging 8b22efb Make fStartedNewLine an std::atomic_bool (Matt Corallo) 507145d Fix race when accessing std::locale::classic() (Matt Corallo) --- src/util.cpp | 6 +++--- src/utiltime.cpp | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/util.cpp b/src/util.cpp index 2f985035a2..41ea3813d7 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -296,7 +296,7 @@ bool LogAcceptCategory(const char* category) * suppress printing of the timestamp when multiple calls are made that don't * end in a newline. Initialize it to true, and hold/manage it, in the calling context. */ -static std::string LogTimestampStr(const std::string &str, bool *fStartedNewLine) +static std::string LogTimestampStr(const std::string &str, std::atomic_bool *fStartedNewLine) { string strStamped; @@ -320,7 +320,7 @@ static std::string LogTimestampStr(const std::string &str, bool *fStartedNewLine * suppress printing of the thread name when multiple calls are made that don't * end in a newline. Initialize it to true, and hold/manage it, in the calling context. */ -static std::string LogThreadNameStr(const std::string &str, bool *fStartedNewLine) +static std::string LogThreadNameStr(const std::string &str, std::atomic_bool *fStartedNewLine) { string strThreadLogged; @@ -340,7 +340,7 @@ static std::string LogThreadNameStr(const std::string &str, bool *fStartedNewLin int LogPrintStr(const std::string &str) { int ret = 0; // Returns total number of characters written - static bool fStartedNewLine = true; + static std::atomic_bool fStartedNewLine(true); std::string strThreadLogged = LogThreadNameStr(str, &fStartedNewLine); std::string strTimestamped = LogTimestampStr(strThreadLogged, &fStartedNewLine); diff --git a/src/utiltime.cpp b/src/utiltime.cpp index 464dec2da8..3ebd1809f0 100644 --- a/src/utiltime.cpp +++ b/src/utiltime.cpp @@ -80,8 +80,9 @@ void MilliSleep(int64_t n) std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime) { + static std::locale classic(std::locale::classic()); // std::locale takes ownership of the pointer - std::locale loc(std::locale::classic(), new boost::posix_time::time_facet(pszFormat)); + std::locale loc(classic, new boost::posix_time::time_facet(pszFormat)); std::stringstream ss; ss.imbue(loc); ss << boost::posix_time::from_time_t(nTime);