NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: bin/45257: ping(8) prints bogus round-trip times after Year 2038



>Fix:
pointed out from dsl@ about the seconds wrap case,
and reviewed by tsutsui@.


Index: ping.c
===================================================================
RCS file: /cvsroot/src/sbin/ping/ping.c,v
retrieving revision 1.94
diff -a -u -r1.94 ping.c
--- ping.c      9 Aug 2011 12:55:19 -0000       1.94
+++ ping.c      18 Aug 2011 10:41:23 -0000
@@ -137,7 +137,7 @@
 #define TST(seq) (A(seq) & B(seq))
 
 struct tv32 {
-       int32_t tv32_sec;
+       uint32_t tv32_sec;
        int32_t tv32_usec;
 };
 
@@ -213,6 +213,7 @@
 static void fill(void);
 static void rnd_fill(void);
 static double diffsec(struct timeval *, struct timeval *);
+static double diffsec_tv32(struct timeval *, struct tv32 *);
 static void timevaladd(struct timeval *, struct timeval *);
 static void sec_to_timeval(const double, struct timeval *);
 static double timeval_to_sec(const struct timeval *);
@@ -859,7 +860,7 @@
 
        opack_icmp.icmp_type = ICMP_ECHO;
        opack_icmp.icmp_id = ident;
-       tv32.tv32_sec = htonl(now.tv_sec);
+       tv32.tv32_sec = htonl((uint32_t)now.tv_sec);
        tv32.tv32_usec = htonl(now.tv_usec);
        if (pingflags & F_TIMING)
                (void) memcpy(&opack_icmp.icmp_data[0], &tv32, sizeof(tv32));
@@ -991,13 +992,12 @@
                        first_rx = last_rx;
                nreceived++;
                if (pingflags & F_TIMING) {
-                       struct timeval tv;
                        struct tv32 tv32;
 
                        (void) memcpy(&tv32, icp->icmp_data, sizeof(tv32));
-                       tv.tv_sec = ntohl(tv32.tv32_sec);
-                       tv.tv_usec = ntohl(tv32.tv32_usec);
-                       triptime = diffsec(&last_rx, &tv);
+                       NTOHL(tv32.tv32_sec);
+                       NTOHL(tv32.tv32_usec);
+                       triptime = diffsec_tv32(&last_rx, &tv32);
                        tsum += triptime;
                        tsumsq += triptime * triptime;
                        if (triptime < tmin)
@@ -1246,6 +1246,14 @@
                + (timenow->tv_usec - then->tv_usec)/1000000.0);
 }
 
+static double
+diffsec_tv32(struct timeval *timenow, struct tv32 *then)
+{
+       /* consider 32bit seconds wrap */
+       return ((uint32_t)timenow->tv_sec - then->tv32_sec) * 1.0 +
+           (timenow->tv_usec - then->tv32_usec) / 1000000.0;
+}
+
 
 static void
 timevaladd(struct timeval *t1,



--
ryo shimizu



Home | Main Index | Thread Index | Old Index