Skip to content

Commit c79e52a

Browse files
committed
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)
2 parents 3bf06e9 + 8b22efb commit c79e52a

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

src/util.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ bool LogAcceptCategory(const char* category)
258258
* suppress printing of the timestamp when multiple calls are made that don't
259259
* end in a newline. Initialize it to true, and hold it, in the calling context.
260260
*/
261-
static std::string LogTimestampStr(const std::string &str, bool *fStartedNewLine)
261+
static std::string LogTimestampStr(const std::string &str, std::atomic_bool *fStartedNewLine)
262262
{
263263
string strStamped;
264264

@@ -285,7 +285,7 @@ static std::string LogTimestampStr(const std::string &str, bool *fStartedNewLine
285285
int LogPrintStr(const std::string &str)
286286
{
287287
int ret = 0; // Returns total number of characters written
288-
static bool fStartedNewLine = true;
288+
static std::atomic_bool fStartedNewLine(true);
289289

290290
string strTimestamped = LogTimestampStr(str, &fStartedNewLine);
291291

src/utiltime.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ void MilliSleep(int64_t n)
7474

7575
std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
7676
{
77+
static std::locale classic(std::locale::classic());
7778
// std::locale takes ownership of the pointer
78-
std::locale loc(std::locale::classic(), new boost::posix_time::time_facet(pszFormat));
79+
std::locale loc(classic, new boost::posix_time::time_facet(pszFormat));
7980
std::stringstream ss;
8081
ss.imbue(loc);
8182
ss << boost::posix_time::from_time_t(nTime);

0 commit comments

Comments
 (0)