From: Lennart Poettering Date: Mon, 25 Jun 2018 12:29:25 +0000 (+0200) Subject: timedate: use gmtime_r() and localtime_r() X-Git-Tag: v240~1040 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e46acb7950a9f07ac60d772309de842c444ad2bd;p=platform%2Fupstream%2Fsystemd.git 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/ --- diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c index 82eb213..5cb13bf 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");