]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
prettydate.c, caljulian.c, calyearstart.c:
authorJohannes Maximilian Kuehn <kuehn@ntp.org>
Tue, 25 Nov 2008 21:23:21 +0000 (21:23 +0000)
committerJohannes Maximilian Kuehn <kuehn@ntp.org>
Tue, 25 Nov 2008 21:23:21 +0000 (21:23 +0000)
  Added changes from Juergen

bk: 492c6cc9Y5c33PnF5ZqClAC-9NG4wA

libntp/caljulian.c
libntp/calyearstart.c
libntp/prettydate.c

index 68ac6b3f0fcd2e3ea6d3e4995507955e6efca23a..7125d0a9462319de3ed91a57ad154b39ed4d054b 100644 (file)
@@ -9,10 +9,6 @@
 #include "ntp_fp.h"
 #include "ntp_unixtime.h"
 
-#ifdef HAVE_LIMITS_H
-# include <limits.h>
-#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
index a6af054025a9b58bc88a9f2bff97d95a9fbb0501..c5a63f729b399f38f9f7c5b017200700eff425eb 100644 (file)
@@ -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);
 }
index eba71a901c286098f70fe024afecbd304fdd8c79..2899ea52d4e39980230c3f1bdef6f6576b6ec808 100644 (file)
@@ -9,10 +9,6 @@
 #include "ntp_stdlib.h"
 #include "ntp_assert.h"
 
-#ifdef HAVE_LIMITS_H
-# include <limits.h>
-#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;