From: Miroslav Lichvar Date: Tue, 19 Jul 2016 12:46:17 +0000 (+0200) Subject: util: round up when converting to 32-bit NTP values X-Git-Tag: 3.0-pre1~159 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7ffe59a7344b480319052de667176bf25fb052ba;p=thirdparty%2Fchrony.git util: round up when converting to 32-bit NTP values NTP clients shouldn't get root delay and dispersion smaller than the server's values. --- diff --git a/util.c b/util.c index 9085ff6b..895fbe02 100644 --- a/util.c +++ b/util.c @@ -632,11 +632,22 @@ UTI_Int32ToDouble(NTP_int32 x) NTP_int32 UTI_DoubleToInt32(double x) { - if (x > MAX_NTP_INT32) - x = MAX_NTP_INT32; - else if (x < 0) - x = 0.0; - return htonl((NTP_int32)(0.5 + 65536.0 * x)); + NTP_int32 r; + + if (x >= MAX_NTP_INT32) { + r = 0xffffffff; + } else if (x <= 0.0) { + r = 0; + } else { + x *= 65536.0; + r = x; + + /* Round up */ + if (r < x) + r++; + } + + return htonl(r); } /* ================================================== */