ff2b41
From 52df8972d4005b23f7c2e52b0cf7210b8c594876 Mon Sep 17 00:00:00 2001
ff2b41
From: Lennart Poettering <lennart@poettering.net>
ff2b41
Date: Mon, 25 Jun 2018 14:29:25 +0200
ff2b41
Subject: [PATCH] timedate: use gmtime_r() and localtime_r()
ff2b41
ff2b41
gmtime() and localtime() operate on a static buffer. let's avoid this,
ff2b41
as we never know whether some library might use these calls in some
ff2b41
backrgound thread.
ff2b41
ff2b41
Discovered by lgtm:
ff2b41
ff2b41
https://lgtm.com/projects/g/systemd/systemd/
ff2b41
(cherry picked from commit e46acb7950a9f07ac60d772309de842c444ad2bd)
ff2b41
ff2b41
Resolves: #1694605
ff2b41
---
ff2b41
 src/timedate/timedated.c | 20 ++++++++++----------
ff2b41
 1 file changed, 10 insertions(+), 10 deletions(-)
ff2b41
ff2b41
diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c
ff2b41
index 09d0dbabcd..afd54a8936 100644
ff2b41
--- a/src/timedate/timedated.c
ff2b41
+++ b/src/timedate/timedated.c
ff2b41
@@ -542,9 +542,9 @@ static int method_set_local_rtc(sd_bus *bus, sd_bus_message *m, void *userdata,
ff2b41
                  * initialize the timezone fields of
ff2b41
                  * struct tm. */
ff2b41
                 if (c->local_rtc)
ff2b41
-                        tm = *localtime(&ts.tv_sec);
ff2b41
+                        localtime_r(&ts.tv_sec, &tm;;
ff2b41
                 else
ff2b41
-                        tm = *gmtime(&ts.tv_sec);
ff2b41
+                        gmtime_r(&ts.tv_sec, &tm;;
ff2b41
 
ff2b41
                 /* Override the main fields of
ff2b41
                  * struct tm, but not the timezone
ff2b41
@@ -562,15 +562,15 @@ static int method_set_local_rtc(sd_bus *bus, sd_bus_message *m, void *userdata,
ff2b41
                 }
ff2b41
 
ff2b41
         } else {
ff2b41
-                struct tm *tm;
ff2b41
+                struct tm tm;
ff2b41
 
ff2b41
                 /* Sync RTC from system clock */
ff2b41
                 if (c->local_rtc)
ff2b41
-                        tm = localtime(&ts.tv_sec);
ff2b41
+                        localtime_r(&ts.tv_sec, &tm;;
ff2b41
                 else
ff2b41
-                        tm = gmtime(&ts.tv_sec);
ff2b41
+                        gmtime_r(&ts.tv_sec, &tm;;
ff2b41
 
ff2b41
-                clock_set_hwclock(tm);
ff2b41
+                clock_set_hwclock(&tm;;
ff2b41
         }
ff2b41
 
ff2b41
         log_info("RTC configured to %s time.", c->local_rtc ? "local" : "UTC");
ff2b41
@@ -585,7 +585,7 @@ static int method_set_time(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bu
ff2b41
         Context *c = userdata;
ff2b41
         int64_t utc;
ff2b41
         struct timespec ts;
ff2b41
-        struct tm* tm;
ff2b41
+        struct tm tm;
ff2b41
         int r;
ff2b41
 
ff2b41
         assert(bus);
ff2b41
@@ -633,10 +633,10 @@ static int method_set_time(sd_bus *bus, sd_bus_message *m, void *userdata, sd_bu
ff2b41
 
ff2b41
         /* Sync down to RTC */
ff2b41
         if (c->local_rtc)
ff2b41
-                tm = localtime(&ts.tv_sec);
ff2b41
+                localtime_r(&ts.tv_sec, &tm;;
ff2b41
         else
ff2b41
-                tm = gmtime(&ts.tv_sec);
ff2b41
-        clock_set_hwclock(tm);
ff2b41
+                gmtime_r(&ts.tv_sec, &tm;;
ff2b41
+        clock_set_hwclock(&tm;;
ff2b41
 
ff2b41
         log_struct(LOG_INFO,
ff2b41
                    LOG_MESSAGE_ID(SD_MESSAGE_TIME_CHANGE),