From: Juergen Perlinger Date: Sun, 28 Jun 2020 06:39:28 +0000 (+0200) Subject: [Bug 3672] fix biased selection in median cut X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=eb509f9a2df1474ce838da4f12f57613c5a76181;p=thirdparty%2Fntp.git [Bug 3672] fix biased selection in median cut bk: 5ef83b20C0U4aqQyHOKWkS-wJ8fjLw --- diff --git a/ChangeLog b/ChangeLog index eeceaa9f1..e49745ab7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +--- +* [Bug 3672] fix biased selection in median cut + --- (4.2.8p15) 2020/06/23 Released by Harlan Stenn diff --git a/ntpd/ntp_refclock.c b/ntpd/ntp_refclock.c index 872f4a052..33ffb90ce 100644 --- a/ntpd/ntp_refclock.c +++ b/ntpd/ntp_refclock.c @@ -583,7 +583,6 @@ refclock_sample( { size_t i, j, k, m, n; double off[MAXSTAGE]; - double offset; /* * Copy the raw offsets and sort into ascending order. Don't do @@ -601,12 +600,16 @@ refclock_sample( /* * Reject the furthest from the median of the samples until * approximately 60 percent of the samples remain. + * + * [Bug 3672] The elimination is now based on the proper + * definition of the median. The true median is not calculated + * directly, though. */ i = 0; j = n; m = n - (n * 4) / 10; - while ((j - i) > m) { - offset = off[(j + i) / 2]; - if (off[j - 1] - offset < offset - off[i]) + while ((k = j - i) > m) { + k = (k - 1) >> 1; + if ((off[j - 1] - off[j - k - 1]) < (off[i + k] - off[i])) i++; /* reject low end */ else j--; /* reject high end */