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.
This commit is contained in:
Miroslav Lichvar 2016-07-19 14:46:17 +02:00
parent 6cd558398a
commit 7ffe59a734

21
util.c
View file

@ -632,11 +632,22 @@ UTI_Int32ToDouble(NTP_int32 x)
NTP_int32 NTP_int32
UTI_DoubleToInt32(double x) UTI_DoubleToInt32(double x)
{ {
if (x > MAX_NTP_INT32) NTP_int32 r;
x = MAX_NTP_INT32;
else if (x < 0) if (x >= MAX_NTP_INT32) {
x = 0.0; r = 0xffffffff;
return htonl((NTP_int32)(0.5 + 65536.0 * x)); } else if (x <= 0.0) {
r = 0;
} else {
x *= 65536.0;
r = x;
/* Round up */
if (r < x)
r++;
}
return htonl(r);
} }
/* ================================================== */ /* ================================================== */