Skip to content

Commit 0d4083e

Browse files
committed
Update ND_BYTES_AVAILABLE_AFTER() macro for better accuracy
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 07a7f33.
1 parent fb59931 commit 0d4083e

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

netdissect.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ nd_trunc_longjmp(netdissect_options *ndo)
391391
* Number of bytes remaining in the captured data, starting at the
392392
* byte pointed to by the argument.
393393
*/
394-
#define ND_BYTES_AVAILABLE_AFTER(p) ND_BYTES_BETWEEN((p), ndo->ndo_snapend)
394+
#define ND_BYTES_AVAILABLE_AFTER(p) ((const u_char *)(p) < ndo->ndo_packetp ? 0 : ND_BYTES_BETWEEN((p), ndo->ndo_snapend))
395395

396396
/*
397397
* Check (expression_1 operator expression_2) for invalid packet with

print-ascii.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ ascii_print(netdissect_options *ndo,
6767
int truncated = FALSE;
6868

6969
ndo->ndo_protocol = "ascii";
70-
caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
70+
caplength = ND_BYTES_AVAILABLE_AFTER(cp);
7171
if (length > caplength) {
7272
length = caplength;
7373
truncated = TRUE;
@@ -113,7 +113,7 @@ hex_and_ascii_print_with_offset(netdissect_options *ndo, const char *indent,
113113
char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp;
114114
char asciistuff[ASCII_LINELENGTH+1], *asp;
115115

116-
caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
116+
caplength = ND_BYTES_AVAILABLE_AFTER(cp);
117117
if (length > caplength) {
118118
length = caplength;
119119
truncated = TRUE;
@@ -181,7 +181,7 @@ hex_print_with_offset(netdissect_options *ndo,
181181
u_int nshorts;
182182
int truncated = FALSE;
183183

184-
caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
184+
caplength = ND_BYTES_AVAILABLE_AFTER(cp);
185185
if (length > caplength) {
186186
length = caplength;
187187
truncated = TRUE;

0 commit comments

Comments
 (0)