Skip to content

Commit

Permalink
Fix issues when getting timezone string
Browse files Browse the repository at this point in the history
Summary:
Always set the time_t variable so it is valid. This is needed because
MSVC will set localtime to null if the variable is invalid.

In addition, while looking back at this code, I realised that
localtime is writing to some internal shared buffer. Access to this
buffer is not guaranteed to be thread safe. We should create our own
buffer and use localtime_r/localtime_s instead.

Reviewed By: mhorowitz

Differential Revision: D23919559

fbshipit-source-id: 8b679936c4c9467f19eb5f56752972c85601af95
  • Loading branch information
neildhar authored and facebook-github-bot committed Sep 26, 2020
1 parent c04d69e commit 5a0f9ea
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/Platform/Unicode/PlatformUnicodeICU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ void dateFormat(
// directly reading tzname at the same time may read the null value, causing
// a segfault.
char tz[50];
time_t t;
struct tm *timeInfo;
timeInfo = localtime(&t);
auto numChars = strftime(tz, sizeof(tz), "%Z", timeInfo);
time_t t = time(nullptr);
struct tm timeInfo;
#ifdef _WINDOWS
localtime_s(&timeInfo, &t);
#else
localtime_r(&t, &timeInfo);
#endif
auto numChars = strftime(tz, sizeof(tz), "%Z", &timeInfo);
(void)numChars;
assert(numChars < sizeof(tz) && "Chars written must fit within buffer");

Expand Down

0 comments on commit 5a0f9ea

Please sign in to comment.