Skip to contentSkip to conversation

system/rtc: Fix a possible year-2038 integer overflow problem

PATCH In review Thomas Huth 3 participants 3 messages May 18

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 system/rtc: Fix a possible year-2038 integer overflow problem

From: Thomas Huth <[email protected]>

rtc_realtime_clock_offset is initialized with:

  rtc_realtime_clock_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;

And QEMU_CLOCK_REALTIME might be based on gettimeofday() in certain cases (see get_clock_realtime() in include/qemu/timer.h). So this counter will exceed 32 bits in the year 2038, thus we should not store this value in a normal integer variable. Change it to a time_t to fix the problem.
And while we're at it, also adjust the nearby rtc_host_datetime_offset variable to be on the safe side in the related code.

---
v2: Use time_t instead of int64_t

 system/rtc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
system/rtc.c +2 -2
index f13dd392a76..018609a4f9a 100644
@@ -41,8 +41,8 @@ static enum {
     RTC_BASE_DATETIME,
 } rtc_base_type = RTC_BASE_UTC;
 static time_t rtc_ref_start_datetime;
-static int rtc_realtime_clock_offset; /* used only with QEMU_CLOCK_REALTIME */
-static int rtc_host_datetime_offset = -1; /* valid & used only with
+static time_t rtc_realtime_clock_offset; /* used only with QEMU_CLOCK_REALTIME */
+static time_t rtc_host_datetime_offset = -1; /* valid & used only with
                                              RTC_BASE_DATETIME */
 QEMUClockType rtc_clock;
 /***********************************************************/
Signature · 1 line
2.54.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 14 lines · Thomas Huth

From: Thomas Huth <[email protected]>

rtc_realtime_clock_offset is initialized with:

  rtc_realtime_clock_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;

And QEMU_CLOCK_REALTIME might be based on gettimeofday() in certain cases (see get_clock_realtime() in include/qemu/timer.h). So this counter will exceed 32 bits in the year 2038, thus we should not store this value in a normal integer variable. Change it to a time_t to fix the problem.
And while we're at it, also adjust the nearby rtc_host_datetime_offset variable to be on the safe side in the related code.

Quoted 6 lines

---
v2: Use time_t instead of int64_t

 system/rtc.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com       ~~        https://hachyderm.io/@berrange :|
|: https://libvirt.org          ~~          https://entangle-photo.org :|
|: https://pixelfed.art/berrange   ~~    https://fstop138.berrange.com :|
Reviewed-by on v2 1/1
#2 lore raw
In reply to Thomas Huth

Le 18/05/2026 à 18:34, Thomas Huth a écrit :

Quoted 26 lines

From: Thomas Huth <[email protected]>

rtc_realtime_clock_offset is initialized with:

   rtc_realtime_clock_offset = qemu_clock_get_ms(QEMU_CLOCK_REALTIME) / 1000;

And QEMU_CLOCK_REALTIME might be based on gettimeofday() in certain cases (see get_clock_realtime() in include/qemu/timer.h). So this counter will exceed 32 bits in the year 2038, thus we should not store this value in a normal integer variable. Change it to a time_t to fix the problem.
And while we're at it, also adjust the nearby rtc_host_datetime_offset variable to be on the safe side in the related code.

---
v2: Use time_t instead of int64_t

  system/rtc.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
system/rtc.c +0 -0
index f13dd392a76..018609a4f9a 100644

system/rtc.c static enum { · line 48

Quoted 10 lines
system/rtc.c +2 -2
@@ -41,8 +41,8 @@ static enum {
      RTC_BASE_DATETIME,
  } rtc_base_type = RTC_BASE_UTC;
  static time_t rtc_ref_start_datetime;
-static int rtc_realtime_clock_offset; /* used only with QEMU_CLOCK_REALTIME */
-static int rtc_host_datetime_offset = -1; /* valid & used only with
+static time_t rtc_realtime_clock_offset; /* used only with QEMU_CLOCK_REALTIME */
+static time_t rtc_host_datetime_offset = -1; /* valid & used only with
                                               RTC_BASE_DATETIME */
  QEMUClockType rtc_clock;
  /***********************************************************/