]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Update ND_BYTES_BETWEEN() macro for better accuracy
authorFrancois-Xavier Le Bail <[email protected]>
Mon, 29 May 2023 17:56:46 +0000 (19:56 +0200)
committerFrancois-Xavier Le Bail <[email protected]>
Tue, 13 Jun 2023 18:57:43 +0000 (20:57 +0200)
Update the macro that computes how many bytes are present, starting
at the first argument and running up to (but not including) the second
argument, and returns that as a u_int (cutting it to 32 bits on LP64
and LLP64 platforms).

This reverses, for reasons of readability, the order of the arguments
which was based on old SMB's PTR_DIFF().

With this change the number of bytes "between" given by the macro is 0
when the first argument is greater than or equal to the second argument.

Update ND_BYTES_AVAILABLE_AFTER() accordingly.

This is a follow-up to f9c2c905b118b69a0b102549c1b25cca871947b5.

(cherry picked from commit fb59931bfca6eed43f8aa0bbec37565fc3c084cb)

netdissect.h
print-bgp.c
print-isoclns.c
print-lwres.c
print-resp.c
print-smb.c
smbutil.c

index b1074ef72bc921fdd7dbcf4d6d59445e11561de9..e9fe9dffda045d21c20d0c9cca8a745d70b3abfe 100644 (file)
@@ -386,13 +386,13 @@ nd_trunc_longjmp(netdissect_options *ndo)
 /*
  * Number of bytes between two pointers.
  */
-#define ND_BYTES_BETWEEN(p1, p2) ((u_int)(((const uint8_t *)(p1)) - (const uint8_t *)(p2)))
+#define ND_BYTES_BETWEEN(p1, p2) ((const u_char *)(p1) >= (const u_char *)(p2) ? 0 : ((u_int)(((const u_char *)(p2)) - (const u_char *)(p1))))
 
 /*
  * Number of bytes remaining in the captured data, starting at the
  * byte pointed to by the argument.
  */
-#define ND_BYTES_AVAILABLE_AFTER(p) ND_BYTES_BETWEEN(ndo->ndo_snapend, (p))
+#define ND_BYTES_AVAILABLE_AFTER(p) ND_BYTES_BETWEEN((p), ndo->ndo_snapend)
 
 /* Check length < minimum for invalid packet with a custom message, format %u */
 #define ND_LCHECKMSG_U(length, minimum, what) \
index 1acdd5baf32bda82e3e02b700f93a3e8dfba0a6d..50bdddb13e2995de42c1042a5926f620974b361b 100644 (file)
@@ -2231,8 +2231,10 @@ bgp_attr_print(netdissect_options *ndo,
             ND_PRINT(", no SNPA");
         }
 
-        add_path4 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 32);
-        add_path6 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 128);
+        add_path4 = check_add_path(ndo, tptr,
+                                   (len-ND_BYTES_BETWEEN(pptr, tptr)), 32);
+        add_path6 = check_add_path(ndo, tptr,
+                                   (len-ND_BYTES_BETWEEN(pptr, tptr)), 128);
 
         while (tptr < pptr + len) {
             advance = bgp_nlri_print(ndo, af, safi, tptr, len, buf, sizeof(buf),
@@ -2258,8 +2260,10 @@ bgp_attr_print(netdissect_options *ndo,
 
         tptr += 3;
 
-        add_path4 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 32);
-        add_path6 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 128);
+        add_path4 = check_add_path(ndo, tptr,
+                                   (len-ND_BYTES_BETWEEN(pptr, tptr)), 32);
+        add_path6 = check_add_path(ndo, tptr,
+                                   (len-ND_BYTES_BETWEEN(pptr, tptr)), 128);
 
         while (tptr < pptr + len) {
             advance = bgp_nlri_print(ndo, af, safi, tptr, len, buf, sizeof(buf),
index 648ae3e0974a33cf4ca0d8f61617738202630ee7..df11797209dd4f1565e5e2182fb0194d28bf6b91 100644 (file)
@@ -1125,9 +1125,10 @@ clnp_print(netdissect_options *ndo,
 
         default:
             /* dump the PDU specific data */
-            if (length > ND_BYTES_BETWEEN(pptr, optr)) {
+            if (length > ND_BYTES_BETWEEN(optr, pptr)) {
                 ND_PRINT("\n\t  undecoded non-header data, length %u", length-li);
-                print_unknown_data(ndo, pptr, "\n\t  ", length - ND_BYTES_BETWEEN(pptr, optr));
+                print_unknown_data(ndo, pptr, "\n\t  ",
+                                   length - ND_BYTES_BETWEEN(optr, pptr));
             }
         }
 
index 97f176cb30054515a1da88ec6f5e07610833646c..bec6318acd21b27b52918ceda14acbcbcedffe97 100644 (file)
@@ -267,7 +267,7 @@ lwres_printaddr(netdissect_options *ndo,
                }
        }
 
-       return ND_BYTES_BETWEEN(p, p0);
+       return ND_BYTES_BETWEEN(p0, p);
 }
 
 void
@@ -546,7 +546,7 @@ lwres_print(netdissect_options *ndo,
                ND_PRINT(" [len: %u != %u]", GET_BE_U_4(np->length),
                          length);
        }
-       if (!unsupported && ND_BYTES_BETWEEN(s, bp) < GET_BE_U_4(np->length))
+       if (!unsupported && ND_BYTES_BETWEEN(bp, s) < GET_BE_U_4(np->length))
                ND_PRINT("[extra]");
        return;
 
index 37a386e3122c96f501266ef688a21beb75d1e180..7388a73fbd573dcb5c4779c9f720d871482a19d4 100644 (file)
@@ -306,7 +306,7 @@ resp_print_string_error_integer(netdissect_options *ndo, const u_char *bp, int l
      * preceding the \r\n.  That includes the opcode, so don't print
      * that.
      */
-    len = ND_BYTES_BETWEEN(bp_ptr, bp);
+    len = ND_BYTES_BETWEEN(bp, bp_ptr);
     RESP_PRINT_SEGMENT(ndo, bp, len);
     ret_len = 1 /*<opcode>*/ + len /*<string>*/ + 2 /*<CRLF>*/;
 
@@ -431,7 +431,7 @@ resp_print_inline(netdissect_options *ndo, const u_char *bp, int length) {
      * Found it; bp_ptr points to the \r or \n, so bp_ptr - bp is the
      * Length of the line text that precedes it.  Print it.
      */
-    len = ND_BYTES_BETWEEN(bp_ptr, bp);
+    len = ND_BYTES_BETWEEN(bp, bp_ptr);
     RESP_PRINT_SEGMENT(ndo, bp, len);
 
     /*
index bcd7363dec955c22a7cb42db4ee8bdd2d26f7fb0..fa7a2c024782d64ab23f34d2c075cfd494988d0a 100644 (file)
@@ -414,7 +414,8 @@ print_negprot(netdissect_options *ndo,
        smb_fdata(ndo, words + 1, f1, ND_MIN(words + 1 + wct * 2, maxbuf),
            unicodestr);
     else
-       smb_data_print(ndo, words + 1, ND_MIN(wct * 2, ND_BYTES_BETWEEN(maxbuf, words + 1)));
+       smb_data_print(ndo, words + 1,
+                       ND_MIN(wct * 2, ND_BYTES_BETWEEN(words + 1, maxbuf)));
 
     bcc = GET_LE_U_2(data);
     ND_PRINT("smb_bcc=%u\n", bcc);
@@ -424,7 +425,7 @@ print_negprot(netdissect_options *ndo,
                                              maxbuf), unicodestr);
        else
            smb_data_print(ndo, data + 2,
-                           ND_MIN(GET_LE_U_2(data), ND_BYTES_BETWEEN(maxbuf, data + 2)));
+                           ND_MIN(GET_LE_U_2(data), ND_BYTES_BETWEEN(data + 2, maxbuf)));
     }
 }
 
@@ -454,7 +455,8 @@ print_sesssetup(netdissect_options *ndo,
        smb_fdata(ndo, words + 1, f1, ND_MIN(words + 1 + wct * 2, maxbuf),
            unicodestr);
     else
-       smb_data_print(ndo, words + 1, ND_MIN(wct * 2, ND_BYTES_BETWEEN(maxbuf, words + 1)));
+       smb_data_print(ndo, words + 1,
+                       ND_MIN(wct * 2, ND_BYTES_BETWEEN(words + 1, maxbuf)));
 
     bcc = GET_LE_U_2(data);
     ND_PRINT("smb_bcc=%u\n", bcc);
@@ -464,7 +466,7 @@ print_sesssetup(netdissect_options *ndo,
                                              maxbuf), unicodestr);
        else
            smb_data_print(ndo, data + 2,
-                           ND_MIN(GET_LE_U_2(data), ND_BYTES_BETWEEN(maxbuf, data + 2)));
+                           ND_MIN(GET_LE_U_2(data), ND_BYTES_BETWEEN(data + 2, maxbuf)));
     }
 }
 
@@ -499,7 +501,7 @@ print_lockingandx(netdissect_options *ndo,
                                              maxbuf), unicodestr);
        else
            smb_data_print(ndo, data + 2,
-                           ND_MIN(GET_LE_U_2(data), ND_BYTES_BETWEEN(maxbuf, data + 2)));
+                           ND_MIN(GET_LE_U_2(data), ND_BYTES_BETWEEN(data + 2, maxbuf)));
     }
 }
 
@@ -871,7 +873,8 @@ print_smb(netdissect_options *ndo,
            } else {
                if (bcc > 0) {
                    ND_PRINT("smb_buf[]=\n");
-                   smb_data_print(ndo, data + 2, ND_MIN(bcc, ND_BYTES_BETWEEN(maxbuf, data + 2)));
+                   smb_data_print(ndo, data + 2,
+                                   ND_MIN(bcc, ND_BYTES_BETWEEN(data + 2, maxbuf)));
                }
            }
        }
@@ -1194,7 +1197,8 @@ nbt_udp137_print(netdissect_options *ndo,
                } else {
                    if (p >= maxbuf)
                        goto out;
-                   smb_data_print(ndo, p, ND_MIN(rdlen, length - ND_BYTES_BETWEEN(p, data)));
+                   smb_data_print(ndo, p,
+                                   ND_MIN(rdlen, length - ND_BYTES_BETWEEN(data, p)));
                    p += rdlen;
                }
            }
index 97217a8d9fc6a8a4def9fbd160b9217e37cd2d92..e512e324d6a056a11853ba362d6e25b3faa3a5b4 100644 (file)
--- a/smbutil.c
+++ b/smbutil.c
@@ -251,7 +251,7 @@ name_len(netdissect_options *ndo,
        s += GET_U_1(s) + 1;
        ND_TCHECK_1(s);
     }
-    return(ND_BYTES_BETWEEN(s, s0) + 1);
+    return(ND_BYTES_BETWEEN(s0, s) + 1);
 
 trunc:
     return(-1);        /* name goes past the end of the buffer */
@@ -334,7 +334,7 @@ write_bits(netdissect_options *ndo,
     u_int i = 0;
 
     while ((p = strchr(fmt, '|'))) {
-       u_int l = ND_BYTES_BETWEEN(p, fmt);
+       u_int l = ND_BYTES_BETWEEN(fmt, p);
        if (l && (val & (1 << i)))
            ND_PRINT("%.*s ", (int)l, fmt);
        fmt = p + 1;
@@ -493,7 +493,7 @@ smb_fdata1(netdissect_options *ndo,
            u_int l;
 
            p = strchr(++fmt, '}');
-           l = ND_BYTES_BETWEEN(p, fmt);
+           l = ND_BYTES_BETWEEN(fmt, p);
 
            if (l > sizeof(bitfmt) - 1)
                l = sizeof(bitfmt)-1;
@@ -742,8 +742,9 @@ smb_fdata1(netdissect_options *ndo,
 
            switch (t) {
            case 1:
-               name_type = name_extract(ndo, startbuf, ND_BYTES_BETWEEN(buf, startbuf),
-                   maxbuf, nbuf);
+               name_type = name_extract(ndo, startbuf,
+                                         ND_BYTES_BETWEEN(startbuf, buf),
+                                         maxbuf, nbuf);
                if (name_type < 0)
                    goto trunc;
                len = name_len(ndo, buf, maxbuf);
@@ -933,7 +934,7 @@ smb_fdata(netdissect_options *ndo,
        }
     }
     if (!depth && buf < maxbuf) {
-       u_int len = ND_BYTES_BETWEEN(maxbuf, buf);
+       u_int len = ND_BYTES_BETWEEN(buf, maxbuf);
        ND_PRINT("Data: (%u bytes)\n", len);
        smb_data_print(ndo, buf, len);
        return(buf + len);