From e46acb7950a9f07ac60d772309de842c444ad2bd Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 25 Jun 2018 14:29:25 +0200 Subject: [PATCH] timedate: use gmtime_r() and localtime_r() gmtime() and localtime() operate on a static buffer. let's avoid this, as we never know whether some library might use these calls in some backrgound thread. Discovered by lgtm: https://lgtm.com/projects/g/systemd/systemd/ --- src/timedate/timedated.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c index 82eb213e95..5cb13bfbb8 100644 --- a/src/timedate/timedated.c +++ b/src/timedate/timedated.c @@ -632,9 +632,9 @@ static int method_set_local_rtc(sd_bus_message *m, void *userdata, sd_bus_error /* Sync system clock from RTC; first, initialize the timezone fields of struct tm. */ if (c->local_rtc) - tm = *localtime(&ts.tv_sec); + localtime_r(&ts.tv_sec, &tm); else - tm = *gmtime(&ts.tv_sec); + gmtime_r(&ts.tv_sec, &tm); /* Override the main fields of struct tm, but not the timezone fields */ r = clock_get_hwclock(&tm); @@ -652,15 +652,15 @@ static int method_set_local_rtc(sd_bus_message *m, void *userdata, sd_bus_error } } else { - struct tm *tm; + struct tm tm; /* Sync RTC from system clock */ if (c->local_rtc) - tm = localtime(&ts.tv_sec); + localtime_r(&ts.tv_sec, &tm); else - tm = gmtime(&ts.tv_sec); + gmtime_r(&ts.tv_sec, &tm); - r = clock_set_hwclock(tm); + r = clock_set_hwclock(&tm); if (r < 0) log_debug_errno(r, "Failed to sync time to hardware clock, ignoring: %m"); } @@ -679,7 +679,7 @@ static int method_set_time(sd_bus_message *m, void *userdata, sd_bus_error *erro int64_t utc; struct timespec ts; usec_t start; - struct tm* tm; + struct tm tm; assert(m); assert(c); @@ -748,11 +748,11 @@ static int method_set_time(sd_bus_message *m, void *userdata, sd_bus_error *erro /* Sync down to RTC */ if (c->local_rtc) - tm = localtime(&ts.tv_sec); + localtime_r(&ts.tv_sec, &tm); else - tm = gmtime(&ts.tv_sec); + gmtime_r(&ts.tv_sec, &tm); - r = clock_set_hwclock(tm); + r = clock_set_hwclock(&tm); if (r < 0) log_debug_errno(r, "Failed to update hardware clock, ignoring: %m");