From: Guy Harris Date: Sat, 3 Feb 2018 02:26:50 +0000 (-0800) Subject: Do time stamp deltas with unsigned arithmetic. X-Git-Tag: tcpdump-4.99-bp~1288 X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/a0f4e3003ee3f6789104727799e005c7f34e51c5 Do time stamp deltas with unsigned arithmetic. First, check whether the originate timestamp is greater than the other timestamp and, if so, calculate (originate - other) and print a - instead of a + before it. This means we're not trying to stuff the difference between two unsigned values into a signed value of the same width (the result of which is undefined if the difference doesn't fit in the signed variable). --- diff --git a/print-ntp.c b/print-ntp.c index 7643ade1..faa31fa4 100644 --- a/print-ntp.c +++ b/print-ntp.c @@ -536,7 +536,7 @@ p_ntp_time(netdissect_options *ndo, ff += FMAXINT; ff = ff / FMAXINT; /* shift radix point by 32 bits */ f = (uint32_t)(ff * 1000000000.0); /* treat fraction as parts per billion */ - ND_PRINT("%u.%09d", i, f); + ND_PRINT("%u.%09u", i, f); #ifdef HAVE_STRFTIME /* @@ -580,9 +580,9 @@ p_ntp_delta(netdissect_options *ndo, const struct l_fixedpt *olfp, const struct l_fixedpt *lfp) { - int32_t i; uint32_t u, uf; uint32_t ou, ouf; + uint32_t i; uint32_t f; double ff; int signbit; @@ -596,20 +596,20 @@ p_ntp_delta(netdissect_options *ndo, return; } - i = u - ou; - - if (i > 0) { /* new is definitely greater than old */ + if (u > ou) { /* new is definitely greater than old */ signbit = 0; + i = u - ou; f = uf - ouf; if (ouf > uf) /* must borrow from high-order bits */ i -= 1; - } else if (i < 0) { /* new is definitely less than old */ + } else if (u < ou) { /* new is definitely less than old */ signbit = 1; + i = ou - u; f = ouf - uf; - if (uf > ouf) /* must carry into the high-order bits */ - i += 1; - i = -i; + if (uf > ouf) /* must borrow from the high-order bits */ + i -= 1; } else { /* int_part is zero */ + i = 0; if (uf > ouf) { signbit = 0; f = uf - ouf; @@ -624,7 +624,7 @@ p_ntp_delta(netdissect_options *ndo, ff += FMAXINT; ff = ff / FMAXINT; /* shift radix point by 32 bits */ f = (uint32_t)(ff * 1000000000.0); /* treat fraction as parts per billion */ - ND_PRINT("%s%d.%09d", signbit ? "-" : "+", i, f); + ND_PRINT("%s%u.%09u", signbit ? "-" : "+", i, f); } /* Prints polling interval in log2 as seconds or fraction of second */