From: Juergen Perlinger Date: Sun, 6 Dec 2015 19:19:32 +0000 (+0100) Subject: [Bug 2772] adj_systime overflows tv_usec X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=61095b89668fc8e1d406dba2645afb41e09efc9e;p=thirdparty%2Fntp.git [Bug 2772] adj_systime overflows tv_usec bk: 56648a44QuJ_xltpef9gRwTLhqD5aA --- diff --git a/ChangeLog b/ChangeLog index b4a8ab765..4a1b12a11 100644 --- a/ChangeLog +++ b/ChangeLog @@ -17,6 +17,7 @@ * CID 1341681: Nits in sntp/tests/keyFile.c. HStenn. * CID 1341682: Nit in libntp/authreadkeys.c. HStenn. * CID 1341684: Nit in tests/ntpd/t-ntp_signd.c. HStenn. +* [Bug 2772] adj_systime overflows tv_usec. perlinger@ntp.org * [Bug 2829] Look at pipe_fds in ntpd.c (did so. perlinger@ntp.org) * [Bug 2887] stratum -1 config results as showing value 99 - fudge stratum only accepts values [0..16]. perlinger@ntp.org diff --git a/libntp/systime.c b/libntp/systime.c index c89d157cb..29f1e8637 100644 --- a/libntp/systime.c +++ b/libntp/systime.c @@ -323,9 +323,18 @@ adj_systime( else quant = 1e-6; ticks = (long)(dtemp / quant + .5); - adjtv.tv_usec = (long)(ticks * quant * 1e6); - dtemp -= adjtv.tv_usec / 1e6; - sys_residual = dtemp; + adjtv.tv_usec = (long)(ticks * quant * 1.e6 + .5); + /* The rounding in the conversions could us push over the + * limits: make sure the result is properly normalised! + * note: sign comes later, all numbers non-negative here. + */ + if (adjtv.tv_usec >= 1000000) { + adjtv.tv_sec += 1; + adjtv.tv_usec -= 1000000; + dtemp -= 1.; + } + /* set the new residual with leftover from correction */ + sys_residual = dtemp - adjtv.tv_usec * 1.e-6; /* * Convert to signed seconds and microseconds for the Unix