]> 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]>
Mon, 12 Jun 2023 14:11:12 +0000 (16:11 +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.

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

index 4f917eebb1eeb25ba86d9f3b8bde013e3aa31b11..09845be6402a436930721d83fb91b081a22e8a81 100644 (file)
@@ -385,13 +385,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 (expression_1 operator expression_2) for invalid packet with
index 694346d733205956ce83733dc7c396eba0c0a3c3..db5b26b0532f21632f8586697eb9c8443a315787 100644 (file)
@@ -2346,8 +2346,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),
@@ -2373,8 +2375,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 f506d4d9fadbb785d71939d877ee46f459c2d2a7..9e5b2234264a14b3dd49239061854e014bb40381 100644 (file)
@@ -1126,9 +1126,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 b8f9b49ae598c3b410de1bfa31032b0b5f54310b..f252275fc34ec8d54fbdcb7fbabbae3e3b11029e 100644 (file)
@@ -267,7 +267,7 @@ lwres_printaddr(netdissect_options *ndo,
                }
        }
 
-       return ND_BYTES_BETWEEN(p, p0);
+       return ND_BYTES_BETWEEN(p0, p);
 }
 
 void
@@ -548,7 +548,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);