rtc: check for gmtime()/localtime() error when setting RTC

Make sure the time conversion succeeded before using the result in
setting of the RTC.
This commit is contained in:
Miroslav Lichvar
2026-01-08 09:42:59 +01:00
parent 007a1ae4fe
commit 911bd54dff

View File

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