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
The following reply was made to PR bin/45257; it has been noted by GNATS.
From: Ryo SHIMIZU <ryo%nerv.org@localhost>
To: gnats-bugs%NetBSD.org@localhost
Cc: gnats-admin%netbsd.org@localhost, netbsd-bugs%netbsd.org@localhost
Subject: Re: bin/45257: ping(8) prints bogus round-trip times after Year 2038
Date: Mon, 15 Aug 2011 17:07:18 +0900
>Fix:
Is this patch ok?
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 15 Aug 2011 07:26:19 -0000
@@ -137,8 +137,8 @@
#define TST(seq) (A(seq) & B(seq))
struct tv32 {
- int32_t tv32_sec;
- int32_t tv32_usec;
+ uint32_t tv32_sec;
+ uint32_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,13 @@
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 +1247,13 @@
+ (timenow->tv_usec - then->tv_usec)/1000000.0);
}
+static double
+diffsec_tv32(struct timeval *timenow, struct tv32 *then)
+{
+ return (((timenow->tv_sec & 0xffffffff) - then->tv32_sec)) * 1.0 +
+ (timenow->tv_usec - then->tv32_usec) / 1000000.0;
+}
+
static void
timevaladd(struct timeval *t1,
I have tested as below;
myhost# cat t.c
#include <stdio.h>
#include <time.h>
int
main(int argc, char *argv[])
{
time_t t;
time(&t);
printf("time_t = %llu = 0x%llx\n", t, t);
return 0;
}
* FYI: 2106-02-07 15:28 JST is 0xfffffff0 in unixtime.
myhost# date
Mon Aug 15 16:56:06 JST 2011
myhost# date 210602071528
Sun Feb 7 15:28:00 JST 2106
myhost# ./a.out
time_t = 4294967281 = 0xfffffff1
myhost# ping localhost
PING localhost (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: icmp_seq=0 ttl=255 time=0.022 ms
64 bytes from 127.0.0.1: icmp_seq=1 ttl=255 time=0.026 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=255 time=0.026 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=255 time=0.028 ms
64 bytes from 127.0.0.1: icmp_seq=4 ttl=255 time=0.046 ms
64 bytes from 127.0.0.1: icmp_seq=5 ttl=255 time=0.030 ms
64 bytes from 127.0.0.1: icmp_seq=6 ttl=255 time=0.026 ms
64 bytes from 127.0.0.1: icmp_seq=7 ttl=255 time=0.025 ms
64 bytes from 127.0.0.1: icmp_seq=8 ttl=255 time=0.026 ms
64 bytes from 127.0.0.1: icmp_seq=9 ttl=255 time=0.025 ms
64 bytes from 127.0.0.1: icmp_seq=10 ttl=255 time=0.024 ms
64 bytes from 127.0.0.1: icmp_seq=11 ttl=255 time=0.025 ms
64 bytes from 127.0.0.1: icmp_seq=12 ttl=255 time=0.028 ms
64 bytes from 127.0.0.1: icmp_seq=13 ttl=255 time=0.041 ms
64 bytes from 127.0.0.1: icmp_seq=14 ttl=255 time=0.026 ms
64 bytes from 127.0.0.1: icmp_seq=15 ttl=255 time=0.048 ms
64 bytes from 127.0.0.1: icmp_seq=16 ttl=255 time=0.028 ms
64 bytes from 127.0.0.1: icmp_seq=17 ttl=255 time=0.026 ms
64 bytes from 127.0.0.1: icmp_seq=18 ttl=255 time=0.028 ms
64 bytes from 127.0.0.1: icmp_seq=19 ttl=255 time=0.025 ms
64 bytes from 127.0.0.1: icmp_seq=20 ttl=255 time=0.025 ms
64 bytes from 127.0.0.1: icmp_seq=21 ttl=255 time=0.027 ms
64 bytes from 127.0.0.1: icmp_seq=22 ttl=255 time=0.029 ms
^C
----localhost PING Statistics----
23 packets transmitted, 23 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.022/0.029/0.048/0.007 ms
myhost# ./a.out
time_t = 4294967305 = 0x100000009
--
ryo shimizu
Home |
Main Index |
Thread Index |
Old Index