[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox-cvs
Subject:    [git commit] ntpd: make NTP client and server Y2036/2038-ready
From:       Denys Vlasenko <vda.linux () googlemail ! com>
Date:       2023-01-17 15:48:54
Message-ID: 20230117160249.9B0D587C46 () busybox ! osuosl ! org
[Download RAW message or body]

commit: https://git.busybox.net/busybox/commit/?id=85acf71d2579ebe4eec05c6f31901adffa700adc
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

The 32-bit integer part of the NTP timestamp overflows in year 2036,
which starts the second NTP era.

Modify the timestamp conversion to shift values between 1900-1970 (in
the first era) to the second era to enable the client to measure its
offset correctly until year 2106 (assuming 64-bit time_t).

Also update the conversion from double used when stepping the clock to
work with 64-bit time_t after reaching the maximum 32-bit value in 2038
and the server conversion to work correctly in the next NTP era.

function                                             old     new   delta
lfp_to_d                                              51      64     +13
step_time                                            326     332      +6
.rodata                                           105260  105264      +4
d_to_lfp                                             100      86     -14
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/1 up/down: 23/-14)              Total: 9 bytes

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
---
 networking/ntpd.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/networking/ntpd.c b/networking/ntpd.c
index 204e1d7c2..4365166ff 100644
--- a/networking/ntpd.c
+++ b/networking/ntpd.c
@@ -554,7 +554,7 @@ gettime1900d(void)
 static void
 d_to_tv(struct timeval *tv, double d)
 {
-	tv->tv_sec = (long)d;
+	tv->tv_sec = (time_t)d;
 	tv->tv_usec = (d - tv->tv_sec) * 1000000;
 }
 
@@ -565,6 +565,14 @@ lfp_to_d(l_fixedpt_t lfp)
 	lfp.int_partl = ntohl(lfp.int_partl);
 	lfp.fractionl = ntohl(lfp.fractionl);
 	ret = (double)lfp.int_partl + ((double)lfp.fractionl / UINT_MAX);
+	/*
+	 * Shift timestamps before 1970 to the second NTP era (2036-2106):
+	 * int_partl value of OFFSET_1900_1970 (2208988800) is interpreted as
+	 * the start of year 1970 and it is the minimal representable time,
+	 * all values form the sequence 2208988800..0xffffffff,0..2208988799.
+	 */
+	if (lfp.int_partl < OFFSET_1900_1970)
+		ret += (double)(1ULL << 32); /* because int_partl is 32-bit wide */
 	return ret;
 }
 static NOINLINE double
@@ -582,8 +590,8 @@ d_to_lfp(l_fixedpt_t *lfp, double d)
 {
 	uint32_t intl;
 	uint32_t frac;
-	intl = (uint32_t)d;
-	frac = (uint32_t)((d - intl) * UINT_MAX);
+	intl = (uint32_t)(time_t)d;
+	frac = (uint32_t)((d - (time_t)d) * UINT_MAX);
 	lfp->int_partl = htonl(intl);
 	lfp->fractionl = htonl(frac);
 }
_______________________________________________
busybox-cvs mailing list
busybox-cvs@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox-cvs
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic