Commit 3c7bff36 authored by ishell's avatar ishell Committed by Commit bot

[base] Use thread safe localtime_r() instead of localtime().

BUG=chromium:631269

Review-Url: https://codereview.chromium.org/2184673002
Cr-Commit-Position: refs/heads/master@{#38168}
parent 8558cbe5
...@@ -46,7 +46,8 @@ static inline void* mmapHelper(size_t len, int prot, int flags, int fildes, ...@@ -46,7 +46,8 @@ static inline void* mmapHelper(size_t len, int prot, int flags, int fildes,
const char* OS::LocalTimezone(double time, TimezoneCache* cache) { const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
if (std::isnan(time)) return ""; if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(floor(time / msPerSecond)); time_t tv = static_cast<time_t>(floor(time / msPerSecond));
struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
if (NULL == t) return ""; if (NULL == t) return "";
return tzname[0]; // The location of the timezone string on AIX. return tzname[0]; // The location of the timezone string on AIX.
} }
...@@ -56,7 +57,8 @@ double OS::LocalTimeOffset(TimezoneCache* cache) { ...@@ -56,7 +57,8 @@ double OS::LocalTimeOffset(TimezoneCache* cache) {
// On AIX, struct tm does not contain a tm_gmtoff field. // On AIX, struct tm does not contain a tm_gmtoff field.
time_t utc = time(NULL); time_t utc = time(NULL);
DCHECK(utc != -1); DCHECK(utc != -1);
struct tm* loc = localtime(&utc); // NOLINT(runtime/threadsafe_fn) struct tm tm;
struct tm* loc = localtime_r(&utc, &tm);
DCHECK(loc != NULL); DCHECK(loc != NULL);
return static_cast<double>((mktime(loc) - utc) * msPerSecond); return static_cast<double>((mktime(loc) - utc) * msPerSecond);
} }
......
...@@ -29,7 +29,8 @@ namespace base { ...@@ -29,7 +29,8 @@ namespace base {
const char* OS::LocalTimezone(double time, TimezoneCache* cache) { const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
if (std::isnan(time)) return ""; if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond)); time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
if (NULL == t) return ""; if (NULL == t) return "";
return tzname[0]; // The location of the timezone string on Cygwin. return tzname[0]; // The location of the timezone string on Cygwin.
} }
...@@ -39,7 +40,8 @@ double OS::LocalTimeOffset(TimezoneCache* cache) { ...@@ -39,7 +40,8 @@ double OS::LocalTimeOffset(TimezoneCache* cache) {
// On Cygwin, struct tm does not contain a tm_gmtoff field. // On Cygwin, struct tm does not contain a tm_gmtoff field.
time_t utc = time(NULL); time_t utc = time(NULL);
DCHECK(utc != -1); DCHECK(utc != -1);
struct tm* loc = localtime(&utc); // NOLINT(runtime/threadsafe_fn) struct tm tm;
struct tm* loc = localtime_r(&utc, &tm);
DCHECK(loc != NULL); DCHECK(loc != NULL);
// time - localtime includes any daylight savings offset, so subtract it. // time - localtime includes any daylight savings offset, so subtract it.
return static_cast<double>((mktime(loc) - utc) * msPerSecond - return static_cast<double>((mktime(loc) - utc) * msPerSecond -
......
...@@ -39,7 +39,8 @@ namespace base { ...@@ -39,7 +39,8 @@ namespace base {
const char* OS::LocalTimezone(double time, TimezoneCache* cache) { const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
if (std::isnan(time)) return ""; if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond)); time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
if (NULL == t) return ""; if (NULL == t) return "";
return t->tm_zone; return t->tm_zone;
} }
...@@ -47,7 +48,8 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) { ...@@ -47,7 +48,8 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
double OS::LocalTimeOffset(TimezoneCache* cache) { double OS::LocalTimeOffset(TimezoneCache* cache) {
time_t tv = time(NULL); time_t tv = time(NULL);
struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
// tm_gmtoff includes any daylight savings offset, so subtract it. // tm_gmtoff includes any daylight savings offset, so subtract it.
return static_cast<double>(t->tm_gmtoff * msPerSecond - return static_cast<double>(t->tm_gmtoff * msPerSecond -
(t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
......
...@@ -96,7 +96,8 @@ bool OS::ArmUsingHardFloat() { ...@@ -96,7 +96,8 @@ bool OS::ArmUsingHardFloat() {
const char* OS::LocalTimezone(double time, TimezoneCache* cache) { const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
if (std::isnan(time)) return ""; if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond)); time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
if (!t || !t->tm_zone) return ""; if (!t || !t->tm_zone) return "";
return t->tm_zone; return t->tm_zone;
} }
...@@ -104,7 +105,8 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) { ...@@ -104,7 +105,8 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
double OS::LocalTimeOffset(TimezoneCache* cache) { double OS::LocalTimeOffset(TimezoneCache* cache) {
time_t tv = time(NULL); time_t tv = time(NULL);
struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
// tm_gmtoff includes any daylight savings offset, so subtract it. // tm_gmtoff includes any daylight savings offset, so subtract it.
return static_cast<double>(t->tm_gmtoff * msPerSecond - return static_cast<double>(t->tm_gmtoff * msPerSecond -
(t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
......
...@@ -102,7 +102,8 @@ void OS::SignalCodeMovingGC() { ...@@ -102,7 +102,8 @@ void OS::SignalCodeMovingGC() {
const char* OS::LocalTimezone(double time, TimezoneCache* cache) { const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
if (std::isnan(time)) return ""; if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond)); time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
if (NULL == t) return ""; if (NULL == t) return "";
return t->tm_zone; return t->tm_zone;
} }
...@@ -110,7 +111,8 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) { ...@@ -110,7 +111,8 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
double OS::LocalTimeOffset(TimezoneCache* cache) { double OS::LocalTimeOffset(TimezoneCache* cache) {
time_t tv = time(NULL); time_t tv = time(NULL);
struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
// tm_gmtoff includes any daylight savings offset, so subtract it. // tm_gmtoff includes any daylight savings offset, so subtract it.
return static_cast<double>(t->tm_gmtoff * msPerSecond - return static_cast<double>(t->tm_gmtoff * msPerSecond -
(t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
......
...@@ -37,7 +37,8 @@ namespace base { ...@@ -37,7 +37,8 @@ namespace base {
const char* OS::LocalTimezone(double time, TimezoneCache* cache) { const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
if (std::isnan(time)) return ""; if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond)); time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
if (NULL == t) return ""; if (NULL == t) return "";
return t->tm_zone; return t->tm_zone;
} }
...@@ -45,7 +46,8 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) { ...@@ -45,7 +46,8 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
double OS::LocalTimeOffset(TimezoneCache* cache) { double OS::LocalTimeOffset(TimezoneCache* cache) {
time_t tv = time(NULL); time_t tv = time(NULL);
struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
// tm_gmtoff includes any daylight savings offset, so subtract it. // tm_gmtoff includes any daylight savings offset, so subtract it.
return static_cast<double>(t->tm_gmtoff * msPerSecond - return static_cast<double>(t->tm_gmtoff * msPerSecond -
(t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
......
...@@ -379,7 +379,8 @@ void OS::ClearTimezoneCache(TimezoneCache* cache) { ...@@ -379,7 +379,8 @@ void OS::ClearTimezoneCache(TimezoneCache* cache) {
double OS::DaylightSavingsOffset(double time, TimezoneCache*) { double OS::DaylightSavingsOffset(double time, TimezoneCache*) {
if (std::isnan(time)) return std::numeric_limits<double>::quiet_NaN(); if (std::isnan(time)) return std::numeric_limits<double>::quiet_NaN();
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond)); time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
if (NULL == t) return std::numeric_limits<double>::quiet_NaN(); if (NULL == t) return std::numeric_limits<double>::quiet_NaN();
return t->tm_isdst > 0 ? 3600 * msPerSecond : 0; return t->tm_isdst > 0 ? 3600 * msPerSecond : 0;
} }
......
...@@ -88,7 +88,8 @@ bool OS::ArmUsingHardFloat() { ...@@ -88,7 +88,8 @@ bool OS::ArmUsingHardFloat() {
const char* OS::LocalTimezone(double time, TimezoneCache* cache) { const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
if (std::isnan(time)) return ""; if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond)); time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
if (NULL == t) return ""; if (NULL == t) return "";
return t->tm_zone; return t->tm_zone;
} }
...@@ -96,7 +97,8 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) { ...@@ -96,7 +97,8 @@ const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
double OS::LocalTimeOffset(TimezoneCache* cache) { double OS::LocalTimeOffset(TimezoneCache* cache) {
time_t tv = time(NULL); time_t tv = time(NULL);
struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
// tm_gmtoff includes any daylight savings offset, so subtract it. // tm_gmtoff includes any daylight savings offset, so subtract it.
return static_cast<double>(t->tm_gmtoff * msPerSecond - return static_cast<double>(t->tm_gmtoff * msPerSecond -
(t->tm_isdst > 0 ? 3600 * msPerSecond : 0)); (t->tm_isdst > 0 ? 3600 * msPerSecond : 0));
......
...@@ -38,7 +38,8 @@ namespace base { ...@@ -38,7 +38,8 @@ namespace base {
const char* OS::LocalTimezone(double time, TimezoneCache* cache) { const char* OS::LocalTimezone(double time, TimezoneCache* cache) {
if (std::isnan(time)) return ""; if (std::isnan(time)) return "";
time_t tv = static_cast<time_t>(std::floor(time/msPerSecond)); time_t tv = static_cast<time_t>(std::floor(time/msPerSecond));
struct tm* t = localtime(&tv); // NOLINT(runtime/threadsafe_fn) struct tm tm;
struct tm* t = localtime_r(&tv, &tm);
if (NULL == t) return ""; if (NULL == t) return "";
return tzname[0]; // The location of the timezone string on Solaris. return tzname[0]; // The location of the timezone string on Solaris.
} }
......
...@@ -46,9 +46,8 @@ inline void MemoryBarrier() { ...@@ -46,9 +46,8 @@ inline void MemoryBarrier() {
int localtime_s(tm* out_tm, const time_t* time) { int localtime_s(tm* out_tm, const time_t* time) {
tm* posix_local_time_struct = localtime(time); // NOLINT tm* posix_local_time_struct = localtime_r(time, out_tm);
if (posix_local_time_struct == NULL) return 1; if (posix_local_time_struct == NULL) return 1;
*out_tm = *posix_local_time_struct;
return 0; return 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment