Skip to content

Commit fb59931

Browse files
committed
Update ND_BYTES_BETWEEN() macro for better accuracy
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 f9c2c90.
1 parent a63600a commit fb59931

File tree

7 files changed

+35
-25
lines changed

7 files changed

+35
-25
lines changed

netdissect.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,13 +385,13 @@ nd_trunc_longjmp(netdissect_options *ndo)
385385
/*
386386
* Number of bytes between two pointers.
387387
*/
388-
#define ND_BYTES_BETWEEN(p1, p2) ((u_int)(((const uint8_t *)(p1)) - (const uint8_t *)(p2)))
388+
#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))))
389389

390390
/*
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(ndo->ndo_snapend, (p))
394+
#define ND_BYTES_AVAILABLE_AFTER(p) ND_BYTES_BETWEEN((p), ndo->ndo_snapend)
395395

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

print-bgp.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2346,8 +2346,10 @@ bgp_attr_print(netdissect_options *ndo,
23462346
ND_PRINT(", no SNPA");
23472347
}
23482348

2349-
add_path4 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 32);
2350-
add_path6 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 128);
2349+
add_path4 = check_add_path(ndo, tptr,
2350+
(len-ND_BYTES_BETWEEN(pptr, tptr)), 32);
2351+
add_path6 = check_add_path(ndo, tptr,
2352+
(len-ND_BYTES_BETWEEN(pptr, tptr)), 128);
23512353

23522354
while (tptr < pptr + len) {
23532355
advance = bgp_nlri_print(ndo, af, safi, tptr, len, buf, sizeof(buf),
@@ -2373,8 +2375,10 @@ bgp_attr_print(netdissect_options *ndo,
23732375

23742376
tptr += 3;
23752377

2376-
add_path4 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 32);
2377-
add_path6 = check_add_path(ndo, tptr, (len-ND_BYTES_BETWEEN(tptr, pptr)), 128);
2378+
add_path4 = check_add_path(ndo, tptr,
2379+
(len-ND_BYTES_BETWEEN(pptr, tptr)), 32);
2380+
add_path6 = check_add_path(ndo, tptr,
2381+
(len-ND_BYTES_BETWEEN(pptr, tptr)), 128);
23782382

23792383
while (tptr < pptr + len) {
23802384
advance = bgp_nlri_print(ndo, af, safi, tptr, len, buf, sizeof(buf),

print-isoclns.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,9 +1126,10 @@ clnp_print(netdissect_options *ndo,
11261126

11271127
default:
11281128
/* dump the PDU specific data */
1129-
if (length > ND_BYTES_BETWEEN(pptr, optr)) {
1129+
if (length > ND_BYTES_BETWEEN(optr, pptr)) {
11301130
ND_PRINT("\n\t undecoded non-header data, length %u", length-li);
1131-
print_unknown_data(ndo, pptr, "\n\t ", length - ND_BYTES_BETWEEN(pptr, optr));
1131+
print_unknown_data(ndo, pptr, "\n\t ",
1132+
length - ND_BYTES_BETWEEN(optr, pptr));
11321133
}
11331134
}
11341135

print-lwres.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ lwres_printaddr(netdissect_options *ndo,
267267
}
268268
}
269269

270-
return ND_BYTES_BETWEEN(p, p0);
270+
return ND_BYTES_BETWEEN(p0, p);
271271
}
272272

273273
void
@@ -548,7 +548,7 @@ lwres_print(netdissect_options *ndo,
548548
ND_PRINT(" [len: %u != %u]", GET_BE_U_4(np->length),
549549
length);
550550
}
551-
if (!unsupported && ND_BYTES_BETWEEN(s, bp) < GET_BE_U_4(np->length))
551+
if (!unsupported && ND_BYTES_BETWEEN(bp, s) < GET_BE_U_4(np->length))
552552
ND_PRINT("[extra]");
553553
return;
554554

print-resp.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ resp_print_string_error_integer(netdissect_options *ndo, const u_char *bp, int l
306306
* preceding the \r\n. That includes the opcode, so don't print
307307
* that.
308308
*/
309-
len = ND_BYTES_BETWEEN(bp_ptr, bp);
309+
len = ND_BYTES_BETWEEN(bp, bp_ptr);
310310
RESP_PRINT_SEGMENT(ndo, bp, len);
311311
ret_len = 1 /*<opcode>*/ + len /*<string>*/ + 2 /*<CRLF>*/;
312312

@@ -431,7 +431,7 @@ resp_print_inline(netdissect_options *ndo, const u_char *bp, int length) {
431431
* Found it; bp_ptr points to the \r or \n, so bp_ptr - bp is the
432432
* Length of the line text that precedes it. Print it.
433433
*/
434-
len = ND_BYTES_BETWEEN(bp_ptr, bp);
434+
len = ND_BYTES_BETWEEN(bp, bp_ptr);
435435
RESP_PRINT_SEGMENT(ndo, bp, len);
436436

437437
/*

print-smb.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,8 @@ print_negprot(netdissect_options *ndo,
414414
smb_fdata(ndo, words + 1, f1, ND_MIN(words + 1 + wct * 2, maxbuf),
415415
unicodestr);
416416
else
417-
smb_data_print(ndo, words + 1, ND_MIN(wct * 2, ND_BYTES_BETWEEN(maxbuf, words + 1)));
417+
smb_data_print(ndo, words + 1,
418+
ND_MIN(wct * 2, ND_BYTES_BETWEEN(words + 1, maxbuf)));
418419

419420
bcc = GET_LE_U_2(data);
420421
ND_PRINT("smb_bcc=%u\n", bcc);
@@ -424,7 +425,7 @@ print_negprot(netdissect_options *ndo,
424425
maxbuf), unicodestr);
425426
else
426427
smb_data_print(ndo, data + 2,
427-
ND_MIN(GET_LE_U_2(data), ND_BYTES_BETWEEN(maxbuf, data + 2)));
428+
ND_MIN(GET_LE_U_2(data), ND_BYTES_BETWEEN(data + 2, maxbuf)));
428429
}
429430
}
430431

@@ -454,7 +455,8 @@ print_sesssetup(netdissect_options *ndo,
454455
smb_fdata(ndo, words + 1, f1, ND_MIN(words + 1 + wct * 2, maxbuf),
455456
unicodestr);
456457
else
457-
smb_data_print(ndo, words + 1, ND_MIN(wct * 2, ND_BYTES_BETWEEN(maxbuf, words + 1)));
458+
smb_data_print(ndo, words + 1,
459+
ND_MIN(wct * 2, ND_BYTES_BETWEEN(words + 1, maxbuf)));
458460

459461
bcc = GET_LE_U_2(data);
460462
ND_PRINT("smb_bcc=%u\n", bcc);
@@ -464,7 +466,7 @@ print_sesssetup(netdissect_options *ndo,
464466
maxbuf), unicodestr);
465467
else
466468
smb_data_print(ndo, data + 2,
467-
ND_MIN(GET_LE_U_2(data), ND_BYTES_BETWEEN(maxbuf, data + 2)));
469+
ND_MIN(GET_LE_U_2(data), ND_BYTES_BETWEEN(data + 2, maxbuf)));
468470
}
469471
}
470472

@@ -499,7 +501,7 @@ print_lockingandx(netdissect_options *ndo,
499501
maxbuf), unicodestr);
500502
else
501503
smb_data_print(ndo, data + 2,
502-
ND_MIN(GET_LE_U_2(data), ND_BYTES_BETWEEN(maxbuf, data + 2)));
504+
ND_MIN(GET_LE_U_2(data), ND_BYTES_BETWEEN(data + 2, maxbuf)));
503505
}
504506
}
505507

@@ -871,7 +873,8 @@ print_smb(netdissect_options *ndo,
871873
} else {
872874
if (bcc > 0) {
873875
ND_PRINT("smb_buf[]=\n");
874-
smb_data_print(ndo, data + 2, ND_MIN(bcc, ND_BYTES_BETWEEN(maxbuf, data + 2)));
876+
smb_data_print(ndo, data + 2,
877+
ND_MIN(bcc, ND_BYTES_BETWEEN(data + 2, maxbuf)));
875878
}
876879
}
877880
}
@@ -1194,7 +1197,8 @@ nbt_udp137_print(netdissect_options *ndo,
11941197
} else {
11951198
if (p >= maxbuf)
11961199
goto out;
1197-
smb_data_print(ndo, p, ND_MIN(rdlen, length - ND_BYTES_BETWEEN(p, data)));
1200+
smb_data_print(ndo, p,
1201+
ND_MIN(rdlen, length - ND_BYTES_BETWEEN(data, p)));
11981202
p += rdlen;
11991203
}
12001204
}

smbutil.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ name_len(netdissect_options *ndo,
251251
s += GET_U_1(s) + 1;
252252
ND_TCHECK_1(s);
253253
}
254-
return(ND_BYTES_BETWEEN(s, s0) + 1);
254+
return(ND_BYTES_BETWEEN(s0, s) + 1);
255255

256256
trunc:
257257
return(-1); /* name goes past the end of the buffer */
@@ -334,7 +334,7 @@ write_bits(netdissect_options *ndo,
334334
u_int i = 0;
335335

336336
while ((p = strchr(fmt, '|'))) {
337-
u_int l = ND_BYTES_BETWEEN(p, fmt);
337+
u_int l = ND_BYTES_BETWEEN(fmt, p);
338338
if (l && (val & (1 << i)))
339339
ND_PRINT("%.*s ", (int)l, fmt);
340340
fmt = p + 1;
@@ -493,7 +493,7 @@ smb_fdata1(netdissect_options *ndo,
493493
u_int l;
494494

495495
p = strchr(++fmt, '}');
496-
l = ND_BYTES_BETWEEN(p, fmt);
496+
l = ND_BYTES_BETWEEN(fmt, p);
497497

498498
if (l > sizeof(bitfmt) - 1)
499499
l = sizeof(bitfmt)-1;
@@ -742,8 +742,9 @@ smb_fdata1(netdissect_options *ndo,
742742

743743
switch (t) {
744744
case 1:
745-
name_type = name_extract(ndo, startbuf, ND_BYTES_BETWEEN(buf, startbuf),
746-
maxbuf, nbuf);
745+
name_type = name_extract(ndo, startbuf,
746+
ND_BYTES_BETWEEN(startbuf, buf),
747+
maxbuf, nbuf);
747748
if (name_type < 0)
748749
goto trunc;
749750
len = name_len(ndo, buf, maxbuf);
@@ -933,7 +934,7 @@ smb_fdata(netdissect_options *ndo,
933934
}
934935
}
935936
if (!depth && buf < maxbuf) {
936-
u_int len = ND_BYTES_BETWEEN(maxbuf, buf);
937+
u_int len = ND_BYTES_BETWEEN(buf, maxbuf);
937938
ND_PRINT("Data: (%u bytes)\n", len);
938939
smb_data_print(ndo, buf, len);
939940
return(buf + len);

0 commit comments

Comments
 (0)