From: Johannes Maximilian Kuehn Date: Tue, 25 Nov 2008 21:23:21 +0000 (+0000) Subject: prettydate.c, caljulian.c, calyearstart.c: X-Git-Tag: NTP_4_2_5P150~1^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dc5e744243f146d66bb6616f74b468efb8e87619;p=thirdparty%2Fntp.git prettydate.c, caljulian.c, calyearstart.c: Added changes from Juergen bk: 492c6cc9Y5c33PnF5ZqClAC-9NG4wA --- diff --git a/libntp/caljulian.c b/libntp/caljulian.c index 68ac6b3f0..7125d0a94 100644 --- a/libntp/caljulian.c +++ b/libntp/caljulian.c @@ -9,10 +9,6 @@ #include "ntp_fp.h" #include "ntp_unixtime.h" -#ifdef HAVE_LIMITS_H -# include -#endif - #if !(defined(ISC_CHECK_ALL) || defined(ISC_CHECK_NONE) || \ defined(ISC_CHECK_ENSURE) || defined(ISC_CHECK_INSIST) || \ defined(ISC_CHECK_INVARIANT)) @@ -58,12 +54,15 @@ caljulian( * non-negative time stamp afterwards. Though at the time of this * writing (2008 A.D.) it would be really strange to have systems * running with clock set to he 1960's or before... + * + * But's important to use a 32 bit max signed value -- LONG_MAX is 64 + * bit on a 64-bit system, and it will give wrong results. */ now = time(NULL); tmplo = (u_int32)now; tmphi = (int32)(now >> 16 >> 16); - M_ADD(tmphi, tmplo, 0, LONG_MAX); + M_ADD(tmphi, tmplo, 0, ((1UL << 31)-1)); /* 32-bit max signed */ M_ADD(tmphi, tmplo, 0, JAN_1970); if ((ntptime > tmplo) && (tmphi > 0)) --tmphi; @@ -73,16 +72,20 @@ caljulian( * Now split into days and seconds-of-day, using the fact that * SECSPERDAY (86400) == 675 * 128; we can get roughly 17000 years of * time scale, using only 32-bit calculations. Some magic numbers here, - * sorry for that. + * sorry for that. (This could be streamlined for 64 bit machines, but + * is worth the trouble?) */ ntptime = tmplo & 127; /* save remainder bits */ tmplo = (tmplo >> 7) | (tmphi << 25); - ntp_day = (u_long)tmplo / 675; - ntptime += ((u_long)tmplo % 675) << 7; + ntp_day = (u_int32)tmplo / 675; + ntptime += ((u_int32)tmplo % 675) << 7; - /* some checks for the algorithm */ + /* some checks for the algorithm + * There's some 64-bit trouble out there: the original NTP time stamp + * had only 32 bits, so our calculation invariant only holds in 32 bits! + */ NTP_ENSURE(ntptime < SECSPERDAY); - NTP_INVARIANT((u_long)(ntptime + ntp_day * SECSPERDAY) == saved_time); + NTP_INVARIANT((u_int32)(ntptime + ntp_day * SECSPERDAY) == (u_int32)saved_time); /* * Do the easy stuff first: take care of hh:mm:ss, ignoring leap diff --git a/libntp/calyearstart.c b/libntp/calyearstart.c index a6af05402..c5a63f729 100644 --- a/libntp/calyearstart.c +++ b/libntp/calyearstart.c @@ -46,8 +46,11 @@ calyearstart(u_long ntp_time) jt.hour = 0; jt.minute = 0; jt.second = 0; - NTP_INVARIANT(caltontp(&jt) + delta == ntp_time); + NTP_INVARIANT((ntp_u_int32_t)(caltontp(&jt) + delta) == (ntp_u_int32_t)ntp_time); # endif - return ntp_time - delta; + /* The NTP time stamps (l_fp) count seconds unsigned mod 2**32, so we + * have to calculate this in the proper way! + */ + return (ntp_u_int32_t)(ntp_time - delta); } diff --git a/libntp/prettydate.c b/libntp/prettydate.c index eba71a901..2899ea52d 100644 --- a/libntp/prettydate.c +++ b/libntp/prettydate.c @@ -9,10 +9,6 @@ #include "ntp_stdlib.h" #include "ntp_assert.h" -#ifdef HAVE_LIMITS_H -# include -#endif - static const char *months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" @@ -63,8 +59,11 @@ ntp2unix_tm( u_int32 dwlo = (int32)t; /* might expand for SIZEOF_TIME_T < 4 */ int32 dwhi = (int32)(t >> 16 >> 16);/* double shift: avoid warnings */ - /* Shift NTP to UN*X epoch, then unfold around currrent time */ - M_ADD(dwhi, dwlo, 0, LONG_MAX); + /* Shift NTP to UN*X epoch, then unfold around currrent time. It's + * important to use a 32 bit max signed value -- LONG_MAX is 64 bit on + * a 64-bit system, and it will give wrong results. + */ + M_ADD(dwhi, dwlo, 0, ((1UL << 31)-1)); /* 32-bit max signed */ if ((ntp -= JAN_1970) > dwlo) --dwhi; dwlo = ntp;