Skip to content

Commit 07a7f33

Browse files
committed
Fix some narrowing warnings on LP64/LLP64 platforms.
Add a ND_BYTES_AVAILABLE_AFTER() macro to find the number of bytes available in the captured data, starting at the byte pointed to by the argument. It returns a u_int rather than a ptrdiff_t, so it'll be 32 bits on LP64 and LLP64 platforms as well as on ILP32 platforms. Use that macro. Make size-of-buffer arguments size_t. Cast some size_t and ptrdiff_t values to u_int or int.
1 parent 7c30120 commit 07a7f33

19 files changed

+54
-47
lines changed

netdissect.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,12 @@ extern void nd_pop_all_buffers(netdissect_options *);
356356
/* Bail out if "*(p)" was not captured */
357357
#define ND_TCHECK_SIZE(p) ND_TCHECK_LEN(p, sizeof(*(p)))
358358

359+
/*
360+
* Number of bytes remaining in the captured data, starting at the
361+
* byte pointed to by the argument.
362+
*/
363+
#define ND_BYTES_AVAILABLE_AFTER(p) ((u_int)(ndo->ndo_snapend - (p)))
364+
359365
#define ND_PRINT(...) (ndo->ndo_printf)(ndo, __VA_ARGS__)
360366
#define ND_DEFAULTPRINT(ap, length) (*ndo->ndo_default_print)(ndo, ap, length)
361367

@@ -731,8 +737,8 @@ extern int mask62plen(const u_char *);
731737
extern const char *dnname_string(netdissect_options *, u_short);
732738
extern const char *dnnum_string(netdissect_options *, u_short);
733739

734-
extern int decode_prefix4(netdissect_options *, const u_char *, u_int, char *, u_int);
735-
extern int decode_prefix6(netdissect_options *, const u_char *, u_int, char *, u_int);
740+
extern int decode_prefix4(netdissect_options *, const u_char *, u_int, char *, size_t);
741+
extern int decode_prefix6(netdissect_options *, const u_char *, u_int, char *, size_t);
736742

737743
extern void esp_print_decodesecret(netdissect_options *);
738744
extern int esp_print_decrypt_buffer_by_ikev2(netdissect_options *, int,

print-ascii.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ ascii_print(netdissect_options *ndo,
6565
u_char s;
6666

6767
ndo->ndo_protocol = "ascii";
68-
caplength = (ndo->ndo_snapend > cp) ? ndo->ndo_snapend - cp : 0;
68+
caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
6969
if (length > caplength)
7070
length = caplength;
7171
ND_PRINT("\n");
@@ -106,7 +106,7 @@ hex_and_ascii_print_with_offset(netdissect_options *ndo, const char *ident,
106106
char hexstuff[HEXDUMP_SHORTS_PER_LINE*HEXDUMP_HEXSTUFF_PER_SHORT+1], *hsp;
107107
char asciistuff[ASCII_LINELENGTH+1], *asp;
108108

109-
caplength = (ndo->ndo_snapend > cp) ? ndo->ndo_snapend - cp : 0;
109+
caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
110110
if (length > caplength)
111111
length = caplength;
112112
nshorts = length / sizeof(u_short);
@@ -169,7 +169,7 @@ hex_print_with_offset(netdissect_options *ndo,
169169
u_int i, s;
170170
u_int nshorts;
171171

172-
caplength = (ndo->ndo_snapend > cp) ? ndo->ndo_snapend - cp : 0;
172+
caplength = (ndo->ndo_snapend > cp) ? ND_BYTES_AVAILABLE_AFTER(cp) : 0;
173173
if (length > caplength)
174174
length = caplength;
175175
nshorts = length / sizeof(u_short);

print-bgp.c

+20-19
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ as_printf(netdissect_options *ndo,
535535

536536
int
537537
decode_prefix4(netdissect_options *ndo,
538-
const u_char *pptr, u_int itemlen, char *buf, u_int buflen)
538+
const u_char *pptr, u_int itemlen, char *buf, size_t buflen)
539539
{
540540
struct in_addr addr;
541541
u_int plen, plenbytes;
@@ -567,7 +567,8 @@ decode_prefix4(netdissect_options *ndo,
567567

568568
static int
569569
decode_labeled_prefix4(netdissect_options *ndo,
570-
const u_char *pptr, u_int itemlen, char *buf, u_int buflen)
570+
const u_char *pptr, u_int itemlen, char *buf,
571+
size_t buflen)
571572
{
572573
struct in_addr addr;
573574
u_int plen, plenbytes;
@@ -672,7 +673,7 @@ bgp_vpn_ip_print(netdissect_options *ndo,
672673
*/
673674
static u_int
674675
bgp_vpn_sg_print(netdissect_options *ndo,
675-
const u_char *pptr, char *buf, u_int buflen)
676+
const u_char *pptr, char *buf, size_t buflen)
676677
{
677678
uint8_t addr_length;
678679
u_int total_length, offset;
@@ -687,7 +688,7 @@ bgp_vpn_sg_print(netdissect_options *ndo,
687688
/* Source address */
688689
ND_TCHECK_LEN(pptr, (addr_length >> 3));
689690
total_length += (addr_length >> 3) + 1;
690-
offset = strlen(buf);
691+
offset = (u_int)strlen(buf);
691692
if (addr_length) {
692693
nd_snprintf(buf + offset, buflen - offset, ", Source %s",
693694
bgp_vpn_ip_print(ndo, pptr, addr_length));
@@ -702,7 +703,7 @@ bgp_vpn_sg_print(netdissect_options *ndo,
702703
/* Group address */
703704
ND_TCHECK_LEN(pptr, (addr_length >> 3));
704705
total_length += (addr_length >> 3) + 1;
705-
offset = strlen(buf);
706+
offset = (u_int)strlen(buf);
706707
if (addr_length) {
707708
nd_snprintf(buf + offset, buflen - offset, ", Group %s",
708709
bgp_vpn_ip_print(ndo, pptr, addr_length));
@@ -901,7 +902,7 @@ decode_rt_routing_info(netdissect_options *ndo,
901902

902903
static int
903904
decode_labeled_vpn_prefix4(netdissect_options *ndo,
904-
const u_char *pptr, char *buf, u_int buflen)
905+
const u_char *pptr, char *buf, size_t buflen)
905906
{
906907
struct in_addr addr;
907908
u_int plen;
@@ -952,7 +953,7 @@ decode_labeled_vpn_prefix4(netdissect_options *ndo,
952953

953954
static int
954955
decode_mdt_vpn_nlri(netdissect_options *ndo,
955-
const u_char *pptr, char *buf, u_int buflen)
956+
const u_char *pptr, char *buf, size_t buflen)
956957
{
957958
const u_char *rd;
958959
const u_char *vpn_ip;
@@ -1027,15 +1028,15 @@ decode_multicast_vpn(netdissect_options *ndo,
10271028
switch(route_type) {
10281029
case BGP_MULTICAST_VPN_ROUTE_TYPE_INTRA_AS_I_PMSI:
10291030
ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN);
1030-
offset = strlen(buf);
1031+
offset = (u_int)strlen(buf);
10311032
nd_snprintf(buf + offset, buflen - offset, ", RD: %s, Originator %s",
10321033
bgp_vpn_rd_print(ndo, pptr),
10331034
bgp_vpn_ip_print(ndo, pptr + BGP_VPN_RD_LEN,
10341035
(route_length - BGP_VPN_RD_LEN) << 3));
10351036
break;
10361037
case BGP_MULTICAST_VPN_ROUTE_TYPE_INTER_AS_I_PMSI:
10371038
ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN + 4);
1038-
offset = strlen(buf);
1039+
offset = (u_int)strlen(buf);
10391040
nd_snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
10401041
bgp_vpn_rd_print(ndo, pptr),
10411042
as_printf(ndo, astostr, sizeof(astostr),
@@ -1044,7 +1045,7 @@ decode_multicast_vpn(netdissect_options *ndo,
10441045

10451046
case BGP_MULTICAST_VPN_ROUTE_TYPE_S_PMSI:
10461047
ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN);
1047-
offset = strlen(buf);
1048+
offset = (u_int)strlen(buf);
10481049
nd_snprintf(buf + offset, buflen - offset, ", RD: %s",
10491050
bgp_vpn_rd_print(ndo, pptr));
10501051
pptr += BGP_VPN_RD_LEN;
@@ -1053,14 +1054,14 @@ decode_multicast_vpn(netdissect_options *ndo,
10531054
addr_length = route_length - sg_length;
10541055

10551056
ND_TCHECK_LEN(pptr, addr_length);
1056-
offset = strlen(buf);
1057+
offset = (u_int)strlen(buf);
10571058
nd_snprintf(buf + offset, buflen - offset, ", Originator %s",
10581059
bgp_vpn_ip_print(ndo, pptr, addr_length << 3));
10591060
break;
10601061

10611062
case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_ACTIVE:
10621063
ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN);
1063-
offset = strlen(buf);
1064+
offset = (u_int)strlen(buf);
10641065
nd_snprintf(buf + offset, buflen - offset, ", RD: %s",
10651066
bgp_vpn_rd_print(ndo, pptr));
10661067
pptr += BGP_VPN_RD_LEN;
@@ -1071,7 +1072,7 @@ decode_multicast_vpn(netdissect_options *ndo,
10711072
case BGP_MULTICAST_VPN_ROUTE_TYPE_SHARED_TREE_JOIN: /* fall through */
10721073
case BGP_MULTICAST_VPN_ROUTE_TYPE_SOURCE_TREE_JOIN:
10731074
ND_TCHECK_LEN(pptr, BGP_VPN_RD_LEN + 4);
1074-
offset = strlen(buf);
1075+
offset = (u_int)strlen(buf);
10751076
nd_snprintf(buf + offset, buflen - offset, ", RD: %s, Source-AS %s",
10761077
bgp_vpn_rd_print(ndo, pptr),
10771078
as_printf(ndo, astostr, sizeof(astostr),
@@ -1117,7 +1118,7 @@ decode_multicast_vpn(netdissect_options *ndo,
11171118

11181119
static int
11191120
decode_labeled_vpn_l2(netdissect_options *ndo,
1120-
const u_char *pptr, char *buf, u_int buflen)
1121+
const u_char *pptr, char *buf, size_t buflen)
11211122
{
11221123
u_int plen, tlen, tlv_type, tlv_len, ttlv_len;
11231124
int stringlen;
@@ -1230,7 +1231,7 @@ decode_labeled_vpn_l2(netdissect_options *ndo,
12301231

12311232
int
12321233
decode_prefix6(netdissect_options *ndo,
1233-
const u_char *pd, u_int itemlen, char *buf, u_int buflen)
1234+
const u_char *pd, u_int itemlen, char *buf, size_t buflen)
12341235
{
12351236
struct in6_addr addr;
12361237
u_int plen, plenbytes;
@@ -1263,7 +1264,7 @@ decode_prefix6(netdissect_options *ndo,
12631264

12641265
static int
12651266
decode_labeled_prefix6(netdissect_options *ndo,
1266-
const u_char *pptr, u_int itemlen, char *buf, u_int buflen)
1267+
const u_char *pptr, u_int itemlen, char *buf, size_t buflen)
12671268
{
12681269
struct in6_addr addr;
12691270
u_int plen, plenbytes;
@@ -1308,7 +1309,7 @@ decode_labeled_prefix6(netdissect_options *ndo,
13081309

13091310
static int
13101311
decode_labeled_vpn_prefix6(netdissect_options *ndo,
1311-
const u_char *pptr, char *buf, u_int buflen)
1312+
const u_char *pptr, char *buf, size_t buflen)
13121313
{
13131314
struct in6_addr addr;
13141315
u_int plen;
@@ -1347,7 +1348,7 @@ decode_labeled_vpn_prefix6(netdissect_options *ndo,
13471348

13481349
static int
13491350
decode_clnp_prefix(netdissect_options *ndo,
1350-
const u_char *pptr, char *buf, u_int buflen)
1351+
const u_char *pptr, char *buf, size_t buflen)
13511352
{
13521353
uint8_t addr[19];
13531354
u_int plen;
@@ -1377,7 +1378,7 @@ decode_clnp_prefix(netdissect_options *ndo,
13771378

13781379
static int
13791380
decode_labeled_vpn_clnp_prefix(netdissect_options *ndo,
1380-
const u_char *pptr, char *buf, u_int buflen)
1381+
const u_char *pptr, char *buf, size_t buflen)
13811382
{
13821383
uint8_t addr[19];
13831384
u_int plen;

print-chdlc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ chdlc_print(netdissect_options *ndo, const u_char *p, u_int length)
114114

115115
trunc:
116116
nd_print_trunc(ndo);
117-
return ndo->ndo_snapend - bp;
117+
return (ND_BYTES_AVAILABLE_AFTER(bp));
118118
}
119119

120120
/*

print-fr.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ fr_print(netdissect_options *ndo,
276276
if (ethertype_print(ndo, extracted_ethertype,
277277
p+addr_len+ETHERTYPE_LEN,
278278
length-addr_len-ETHERTYPE_LEN,
279-
ndo->ndo_snapend-p-addr_len-ETHERTYPE_LEN,
279+
ND_BYTES_AVAILABLE_AFTER(p)-addr_len-ETHERTYPE_LEN,
280280
NULL, NULL) == 0)
281281
/* ether_type not known, probably it wasn't one */
282282
ND_PRINT("UI %02x! ", GET_U_1(p + addr_len));
@@ -333,7 +333,7 @@ fr_print(netdissect_options *ndo,
333333
break;
334334

335335
case NLPID_SNAP:
336-
if (snap_print(ndo, p, length, ndo->ndo_snapend - p, NULL, NULL, 0) == 0) {
336+
if (snap_print(ndo, p, length, ND_BYTES_AVAILABLE_AFTER(p), NULL, NULL, 0) == 0) {
337337
/* ether_type not known, print raw packet */
338338
if (!ndo->ndo_eflag)
339339
fr_hdr_print(ndo, length + hdr_len, hdr_len,

print-geneve.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,9 @@ geneve_print(netdissect_options *ndo, const u_char *bp, u_int len)
224224
else
225225
ND_PRINT("\n\t");
226226

227-
if (ethertype_print(ndo, prot, bp, len, ndo->ndo_snapend - bp, NULL, NULL) == 0) {
227+
if (ethertype_print(ndo, prot, bp, len, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL) == 0) {
228228
if (prot == ETHERTYPE_TEB)
229-
ether_print(ndo, bp, len, ndo->ndo_snapend - bp, NULL, NULL);
229+
ether_print(ndo, bp, len, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL);
230230
else
231231
ND_PRINT("geneve-proto-0x%x", prot);
232232
}

print-gre.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ gre_print_0(netdissect_options *ndo, const u_char *bp, u_int length)
224224
isoclns_print(ndo, bp, len);
225225
break;
226226
case ETHERTYPE_TEB:
227-
ether_print(ndo, bp, len, ndo->ndo_snapend - bp, NULL, NULL);
227+
ether_print(ndo, bp, len, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL);
228228
break;
229229
default:
230230
ND_PRINT("gre-proto-0x%x", prot);

print-lisp.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ lisp_print(netdissect_options *ndo, const u_char *bp, u_int length)
381381
/* Check if packet isn't over yet */
382382
if (packet_iterator + packet_offset < ndo->ndo_snapend) {
383383
hex_print_with_offset(ndo, "\n Data: ", packet_iterator + packet_offset,
384-
(ndo->ndo_snapend - (packet_iterator + packet_offset)), 0);
384+
ND_BYTES_AVAILABLE_AFTER(packet_iterator + packet_offset), 0);
385385
}
386386
}
387387
return;

print-lwres.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ lwres_printname(netdissect_options *ndo,
210210
}
211211
p++; /* skip terminating \0 */
212212

213-
return p - p0;
213+
return (int)(p - p0);
214214

215215
trunc:
216216
return -1;
@@ -254,7 +254,7 @@ lwres_printbinlen(netdissect_options *ndo,
254254
ND_PRINT("%02x", GET_U_1(p));
255255
p++;
256256
}
257-
return p - p0;
257+
return (int)(p - p0);
258258

259259
trunc:
260260
return -1;

print-mobility.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ mobility_print(netdissect_options *ndo,
227227
* returned length, however, as it breaks out of the
228228
* header-processing loop.
229229
*/
230-
mhlen = ep - bp;
230+
mhlen = (unsigned)(ep - bp);
231231
goto trunc;
232232
}
233233
mhlen = (GET_U_1(mh->ip6m_len) + 1) << 3;

print-nsh.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ nsh_print(netdissect_options *ndo, const u_char *bp, u_int len)
170170
ip6_print(ndo, bp, next_len);
171171
break;
172172
case 0x3:
173-
ether_print(ndo, bp, next_len, ndo->ndo_snapend - bp, NULL, NULL);
173+
ether_print(ndo, bp, next_len, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL);
174174
break;
175175
default:
176176
ND_PRINT("ERROR: unknown-next-protocol");

print-olsr.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -519,10 +519,10 @@ olsr_print(netdissect_options *ndo,
519519

520520
case OLSR_MID_MSG:
521521
{
522-
size_t addr_size = sizeof(nd_ipv4);
522+
u_int addr_size = (u_int)sizeof(nd_ipv4);
523523

524524
if (is_ipv6)
525-
addr_size = sizeof(nd_ipv6);
525+
addr_size = (u_int)sizeof(nd_ipv6);
526526

527527
while (msg_tlen >= addr_size) {
528528
ND_TCHECK_LEN(msg_data, addr_size);

print-openflow-1.0.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1128,7 +1128,7 @@ of10_packet_data_print(netdissect_options *ndo,
11281128
ND_TCHECK_LEN(cp, len);
11291129
ndo->ndo_vflag -= 3;
11301130
ND_PRINT(", frame decoding below\n");
1131-
ether_print(ndo, cp, len, ndo->ndo_snapend - cp, NULL, NULL);
1131+
ether_print(ndo, cp, len, ND_BYTES_AVAILABLE_AFTER(cp), NULL, NULL);
11321132
ndo->ndo_vflag += 3;
11331133
return cp + len;
11341134

print-otv.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ otv_print(netdissect_options *ndo, const u_char *bp, u_int len)
6767
ND_TCHECK_1(bp);
6868
bp += 1;
6969

70-
ether_print(ndo, bp, len - OTV_HDR_LEN, ndo->ndo_snapend - bp, NULL, NULL);
70+
ether_print(ndo, bp, len - OTV_HDR_LEN, ND_BYTES_AVAILABLE_AFTER(bp), NULL, NULL);
7171
return;
7272

7373
trunc:

print-ppp.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ handle_chap(netdissect_options *ndo,
906906
ND_PRINT("%02x", GET_U_1(p));
907907
p++;
908908
}
909-
name_size = len - (p - p0);
909+
name_size = len - (u_int)(p - p0);
910910
ND_PRINT(", Name ");
911911
for (i = 0; i < name_size; i++) {
912912
ND_TCHECK_1(p);
@@ -916,7 +916,7 @@ handle_chap(netdissect_options *ndo,
916916
break;
917917
case CHAP_SUCC:
918918
case CHAP_FAIL:
919-
msg_size = len - (p - p0);
919+
msg_size = len - (u_int)(p - p0);
920920
ND_PRINT(", Msg ");
921921
for (i = 0; i< msg_size; i++) {
922922
ND_TCHECK_1(p);
@@ -1443,7 +1443,7 @@ ppp_hdlc(netdissect_options *ndo,
14431443
*/
14441444
se = ndo->ndo_snapend;
14451445
ndo->ndo_snapend = t;
1446-
length = t - b;
1446+
length = ND_BYTES_AVAILABLE_AFTER(b);
14471447

14481448
/* now lets guess about the payload codepoint format */
14491449
if (length < 1)

print-rip.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ rip_print(netdissect_options *ndo,
303303
nd_print_trunc(ndo);
304304
return;
305305
}
306-
len = ndo->ndo_snapend - dat;
306+
len = ND_BYTES_AVAILABLE_AFTER(dat);
307307
if (len > length)
308308
len = length;
309309
if (len < sizeof(*rp)) {

print-telnet.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ telnet_parse(netdissect_options *ndo, const u_char *sp, u_int length, int print)
497497
}
498498

499499
done:
500-
return sp - osp;
500+
return (int)(sp - osp);
501501

502502
trunc:
503503
nd_print_trunc(ndo);
@@ -532,7 +532,7 @@ telnet_print(netdissect_options *ndo, const u_char *sp, u_int length)
532532
if (ndo->ndo_Xflag && 2 < ndo->ndo_vflag) {
533533
if (first)
534534
ND_PRINT("\nTelnet:");
535-
hex_print_with_offset(ndo, "\n", sp, l, sp - osp);
535+
hex_print_with_offset(ndo, "\n", sp, l, (u_int)(sp - osp));
536536
if (l > 8)
537537
ND_PRINT("\n\t\t\t\t");
538538
else

0 commit comments

Comments
 (0)