Skip to content

Commit 5e6fcbc

Browse files
committed
Replace usage of std::ctime, which is not safe for use in multithreaded contexts
- std::ctime returns a pointer to a string that "may be shared between std::asctime and std::ctime, and may be overwritten on each invocation of any of those functions.". - https://en.cppreference.com/w/cpp/chrono/c/ctime - Replaced with call to strftime to generate the same string representation (using the format string: %c) - Leveraged localtime_r (which is thread-safe) to convert time_t to struct tm, as required by strftime.
1 parent 718d121 commit 5e6fcbc

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/utils/string.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
#include <utility>
2222
#include <sstream>
2323
#include <iomanip>
24+
#include <time.h>
25+
26+
#ifdef WIN32
27+
#include "src/compat/msvc.h"
28+
#endif
2429

2530
#ifndef SRC_UTILS_STRING_H_
2631
#define SRC_UTILS_STRING_H_
@@ -60,9 +65,11 @@ const char HEX2DEC[256] = {
6065

6166

6267
inline std::string ascTime(const time_t *t) {
63-
std::string ts = std::ctime(t);
64-
ts.pop_back();
65-
return ts;
68+
struct tm timeinfo;
69+
localtime_r(t, &timeinfo);
70+
char tstr[std::size("Www Mmm dd hh:mm:ss yyyy")];
71+
strftime(tstr, std::size(tstr), "%c", &timeinfo);
72+
return tstr;
6673
}
6774

6875

0 commit comments

Comments
 (0)