From: otto Date: Wed, 15 Nov 2023 15:52:09 +0000 (+0000) Subject: Correct denominators when converting NTP fixed point values to double X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=c338e3e2ad38b099fa13a573170a235f9ed893ce;p=openbsd Correct denominators when converting NTP fixed point values to double and vice-versa; ok tb@ --- diff --git a/usr.sbin/ntpd/ntp.h b/usr.sbin/ntpd/ntp.h index fd35bf93791..20d67f31d3d 100644 --- a/usr.sbin/ntpd/ntp.h +++ b/usr.sbin/ntpd/ntp.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ntp.h,v 1.14 2020/01/30 15:55:41 otto Exp $ */ +/* $OpenBSD: ntp.h,v 1.15 2023/11/15 15:52:09 otto Exp $ */ /* * Copyright (c) 2004 Henning Brauer @@ -43,11 +43,13 @@ struct l_fixedpt { u_int32_t int_partl; u_int32_t fractionl; }; +#define L_DENOMINATOR (UINT32_MAX + 1ULL) struct s_fixedpt { u_int16_t int_parts; u_int16_t fractions; }; +#define S_DENOMINATOR (UINT16_MAX + 1) /* RFC Section 4 * diff --git a/usr.sbin/ntpd/util.c b/usr.sbin/ntpd/util.c index be113ba856b..d490601b04d 100644 --- a/usr.sbin/ntpd/util.c +++ b/usr.sbin/ntpd/util.c @@ -1,4 +1,4 @@ -/* $OpenBSD: util.c,v 1.25 2020/01/30 15:55:41 otto Exp $ */ +/* $OpenBSD: util.c,v 1.26 2023/11/15 15:52:09 otto Exp $ */ /* * Copyright (c) 2004 Alexander Guy @@ -96,7 +96,7 @@ lfp_to_d(struct l_fixedpt lfp) if (lfp.int_partl <= INT32_MAX) base++; ret = base * SECS_IN_ERA; - ret += (double)(lfp.int_partl) + ((double)lfp.fractionl / UINT_MAX); + ret += (double)(lfp.int_partl) + ((double)lfp.fractionl / L_DENOMINATOR); return (ret); } @@ -109,7 +109,7 @@ d_to_lfp(double d) while (d > SECS_IN_ERA) d -= SECS_IN_ERA; lfp.int_partl = htonl((u_int32_t)d); - lfp.fractionl = htonl((u_int32_t)((d - (u_int32_t)d) * UINT_MAX)); + lfp.fractionl = htonl((u_int32_t)((d - (u_int32_t)d) * L_DENOMINATOR)); return (lfp); } @@ -122,7 +122,7 @@ sfp_to_d(struct s_fixedpt sfp) 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 / S_DENOMINATOR); return (ret); } @@ -133,7 +133,7 @@ d_to_sfp(double d) struct s_fixedpt sfp; sfp.int_parts = htons((u_int16_t)d); - sfp.fractions = htons((u_int16_t)((d - (u_int16_t)d) * USHRT_MAX)); + sfp.fractions = htons((u_int16_t)((d - (u_int16_t)d) * S_DENOMINATOR)); return (sfp); }