]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Update ND_BYTES_AVAILABLE_AFTER() macro for better accuracy 1047/head
authorFrancois-Xavier Le Bail <[email protected]>
Mon, 5 Jun 2023 17:50:11 +0000 (19:50 +0200)
committerFrancois-Xavier Le Bail <[email protected]>
Mon, 12 Jun 2023 14:11:12 +0000 (16:11 +0200)
With this change the number of bytes available in the captured data
given by the macro is 0 when the argument is greater than or equal
to ndo_snapend or less than ndo_packetp (e.g. pointer underflow).

i.e.:
If p is like p3, the macro gives 0.
If p is like p1, the macro gives 0.

|------------------|-----------------------|-----------------|
      p1  <   ndo_packetp  <=  p2  <  ndo_snapend   <=   p3

Update the ascii_print(), hex_and_ascii_print_with_offset() and
hex_print_with_offset() functions accordingly.

This is a follow-up to 07a7f33ba3ff346b9ce31772645a06980baca907.

netdissect.h
print-ascii.c

index 09845be6402a436930721d83fb91b081a22e8a81..bff2d52009afe167cf982f0e582ed9c1a721feaa 100644 (file)
@@ -391,7 +391,7 @@ nd_trunc_longjmp(netdissect_options *ndo)
  * Number of bytes remaining in the captured data, starting at the
  * byte pointed to by the argument.
  */
  * 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((p), ndo->ndo_snapend)
+#define ND_BYTES_AVAILABLE_AFTER(p) ((const u_char *)(p) < ndo->ndo_packetp ? 0 : ND_BYTES_BETWEEN((p), ndo->ndo_snapend))
 
 /*
  * Check (expression_1 operator expression_2) for invalid packet with
 
 /*
  * Check (expression_1 operator expression_2) for invalid packet with
index 27cf2df1d07441def5442f2e5ba8fe66d5dd2fc9..ce97444554d0a5a3796c4b281359a6616e14f2ed 100644 (file)
@@ -67,7 +67,7 @@ ascii_print(netdissect_options *ndo,
        int truncated = FALSE;
 
        ndo->ndo_protocol = "ascii";
        int truncated = FALSE;
 
        ndo->ndo_protocol = "ascii";
-       caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
+       caplength = ND_BYTES_AVAILABLE_AFTER(cp);
        if (length > caplength) {
                length = caplength;
                truncated = TRUE;
        if (length > caplength) {
                length = caplength;
                truncated = TRUE;
@@ -113,7 +113,7 @@ hex_and_ascii_print_with_offset(netdissect_options *ndo, const char *indent,
        char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp;
        char asciistuff[ASCII_LINELENGTH+1], *asp;
 
        char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp;
        char asciistuff[ASCII_LINELENGTH+1], *asp;
 
-       caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
+       caplength = ND_BYTES_AVAILABLE_AFTER(cp);
        if (length > caplength) {
                length = caplength;
                truncated = TRUE;
        if (length > caplength) {
                length = caplength;
                truncated = TRUE;
@@ -181,7 +181,7 @@ hex_print_with_offset(netdissect_options *ndo,
        u_int nshorts;
        int truncated = FALSE;
 
        u_int nshorts;
        int truncated = FALSE;
 
-       caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
+       caplength = ND_BYTES_AVAILABLE_AFTER(cp);
        if (length > caplength) {
                length = caplength;
                truncated = TRUE;
        if (length > caplength) {
                length = caplength;
                truncated = TRUE;