]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
rtc: check for gmtime()/localtime() error when setting RTC
authorMiroslav Lichvar <mlichvar@redhat.com>
Thu, 8 Jan 2026 08:42:59 +0000 (09:42 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Mon, 26 Jan 2026 11:33:12 +0000 (12:33 +0100)
Make sure the time conversion succeeded before using the result in
setting of the RTC.

rtc_linux.c

index 08ba5b526ea6e0eb6048bf11577d4c6ab841bb47..01fd843441e4a23361ef4235294e062f91570d9f 100644 (file)
@@ -297,16 +297,22 @@ slew_samples
    corresponding real time clock 'DMY HMS' form, taking account of
    whether the user runs his RTC on the local time zone or UTC */
 
-static void
+static int
 rtc_from_t(const time_t *t, struct rtc_time *rtc_raw, int utc)
 {
   struct tm *rtc_tm;
+
   if (utc) {
     rtc_tm = gmtime(t);
   } else {
     rtc_tm = localtime(t);
   }
 
+  if (!rtc_tm) {
+    DEBUG_LOG("gmtime()/localtime() failed");
+    return 0;
+  }
+
   rtc_raw->tm_sec = rtc_tm->tm_sec;
   rtc_raw->tm_min = rtc_tm->tm_min;
   rtc_raw->tm_hour = rtc_tm->tm_hour;
@@ -316,6 +322,8 @@ rtc_from_t(const time_t *t, struct rtc_time *rtc_raw, int utc)
   rtc_raw->tm_wday = rtc_tm->tm_wday;
   rtc_raw->tm_yday = rtc_tm->tm_yday;
   rtc_raw->tm_isdst = rtc_tm->tm_isdst;
+
+  return 1;
 }
 
 /* ================================================== */
@@ -609,15 +617,11 @@ static void
 set_rtc(time_t new_rtc_time)
 {
   struct rtc_time rtc_raw;
-  int status;
 
-  rtc_from_t(&new_rtc_time, &rtc_raw, rtc_on_utc);
-
-  status = ioctl(rtc_fd, RTC_SET_TIME, &rtc_raw);
-  if (status < 0) {
+  if (!rtc_from_t(&new_rtc_time, &rtc_raw, rtc_on_utc) ||
+      ioctl(rtc_fd, RTC_SET_TIME, &rtc_raw) < 0) {
     LOG(LOGS_ERR, "Could not set RTC time");
   }
-
 }
 
 /* ================================================== */