From 507145d78595e052ce13368e122f72c85093992c Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 27 Nov 2016 15:11:49 -0800 Subject: Fix race when accessing std::locale::classic() See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78552 --- src/utiltime.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/utiltime.cpp b/src/utiltime.cpp index da590f8889..51d545ef8a 100644 --- a/src/utiltime.cpp +++ b/src/utiltime.cpp @@ -74,8 +74,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); -- cgit v1.2.3 From 8b22efb6f7c406951f33a04e84377fd16f02121c Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 27 Nov 2016 15:14:36 -0800 Subject: Make fStartedNewLine an std::atomic_bool While this doesnt really fix the race of adding timestamps mid-logical-line, it avoids the undefined behavior of using a bool in multiple threads. --- src/util.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/util.cpp b/src/util.cpp index c20ede6221..a1f87a2645 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -259,7 +259,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 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; @@ -286,7 +286,7 @@ static std::string LogTimestampStr(const std::string &str, bool *fStartedNewLine 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); string strTimestamped = LogTimestampStr(str, &fStartedNewLine); -- cgit v1.2.3