From c344ca6c7f5d8468624ae518d2912d1d9612cb91 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Tue, 17 Jan 2023 17:01:05 +0100 Subject: ntpd: correct fixed->float conversions of fractions Need to divide by (1<<32), not by (1<<32)-1. Fraction of 0xffffffff is not 1 whole second. function old new delta .rodata 105264 105268 +4 Signed-off-by: Denys Vlasenko --- networking/ntpd.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'networking') diff --git a/networking/ntpd.c b/networking/ntpd.c index 4365166ff..ff49550e9 100644 --- a/networking/ntpd.c +++ b/networking/ntpd.c @@ -564,7 +564,7 @@ lfp_to_d(l_fixedpt_t lfp) double ret; lfp.int_partl = ntohl(lfp.int_partl); lfp.fractionl = ntohl(lfp.fractionl); - ret = (double)lfp.int_partl + ((double)lfp.fractionl / UINT_MAX); + ret = (double)lfp.int_partl + ((double)lfp.fractionl / (1ULL << 32)); /* * Shift timestamps before 1970 to the second NTP era (2036-2106): * int_partl value of OFFSET_1900_1970 (2208988800) is interpreted as @@ -581,7 +581,7 @@ sfp_to_d(s_fixedpt_t sfp) double ret; sfp.int_parts = ntohs(sfp.int_parts); sfp.fractions = ntohs(sfp.fractions); - ret = (double)sfp.int_parts + ((double)sfp.fractions / USHRT_MAX); + ret = (double)sfp.int_parts + ((double)sfp.fractions / (1 << 16)); return ret; } #if ENABLE_FEATURE_NTPD_SERVER @@ -591,7 +591,7 @@ d_to_lfp(l_fixedpt_t *lfp, double d) uint32_t intl; uint32_t frac; intl = (uint32_t)(time_t)d; - frac = (uint32_t)((d - (time_t)d) * UINT_MAX); + frac = (uint32_t)((d - (time_t)d) * 0xffffffff); lfp->int_partl = htonl(intl); lfp->fractionl = htonl(frac); } @@ -601,7 +601,7 @@ d_to_sfp(s_fixedpt_t *sfp, double d) uint16_t ints; uint16_t frac; ints = (uint16_t)d; - frac = (uint16_t)((d - ints) * USHRT_MAX); + frac = (uint16_t)((d - ints) * 0xffff); sfp->int_parts = htons(ints); sfp->fractions = htons(frac); } -- cgit v1.2.3