]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
util: round up when converting to 32-bit NTP values
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 19 Jul 2016 12:46:17 +0000 (14:46 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 20 Jul 2016 07:17:24 +0000 (09:17 +0200)
NTP clients shouldn't get root delay and dispersion smaller than the
server's values.

util.c

diff --git a/util.c b/util.c
index 9085ff6b2436eab54e85fdd6b15e8a753ed2fb1b..895fbe023ad0f83b0b5f0bf4e4cf7747bc6b9adf 100644 (file)
--- 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);
 }
 
 /* ================================================== */