Skip to content

Commit

Permalink
Squelch more narrowing warnings.
Browse files Browse the repository at this point in the history
Add an ND_BYTES_BETWEEN() macro that computes how many bytes are
present, starting at the second argument and running up to (but not
including) the first argument, and returns that as a u_int (cutting it
to 32 bits on LP64 and LLP64 platforms).

Use that, including using it as a replacement for SMB's PTR_DIFF().
  • Loading branch information
guyharris committed Apr 18, 2019
1 parent 07a7f33 commit f9c2c90
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 24 deletions.
7 changes: 6 additions & 1 deletion netdissect.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,16 @@ extern void nd_pop_all_buffers(netdissect_options *);
/* Bail out if "*(p)" was not captured */
#define ND_TCHECK_SIZE(p) ND_TCHECK_LEN(p, sizeof(*(p)))

/*
* Number of bytes between two pointers.
*/
#define ND_BYTES_BETWEEN(p1, p2) ((u_int)(((const uint8_t *)(p1)) - (const uint8_t *)(p2)))

/*
* Number of bytes remaining in the captured data, starting at the
* byte pointed to by the argument.
*/
#define ND_BYTES_AVAILABLE_AFTER(p) ((u_int)(ndo->ndo_snapend - (p)))
#define ND_BYTES_AVAILABLE_AFTER(p) ND_BYTES_BETWEEN(ndo->ndo_snapend, (p))

#define ND_PRINT(...) (ndo->ndo_printf)(ndo, __VA_ARGS__)
#define ND_DEFAULTPRINT(ap, length) (*ndo->ndo_default_print)(ndo, ap, length)
Expand Down
8 changes: 4 additions & 4 deletions print-bgp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1925,8 +1925,8 @@ bgp_attr_print(netdissect_options *ndo,
ND_PRINT(", no SNPA");
}

add_path4 = check_add_path(ndo, tptr, (len-(tptr - pptr)), 32);
add_path6 = check_add_path(ndo, tptr, (len-(tptr - pptr)), 128);
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);

while (tptr < pptr + len) {
switch (af<<8 | safi) {
Expand Down Expand Up @@ -2106,8 +2106,8 @@ bgp_attr_print(netdissect_options *ndo,

tptr += 3;

add_path4 = check_add_path(ndo, tptr, (len-(tptr - pptr)), 32);
add_path6 = check_add_path(ndo, tptr, (len-(tptr - pptr)), 128);
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);

while (tptr < pptr + len) {
switch (af<<8 | safi) {
Expand Down
6 changes: 3 additions & 3 deletions print-lwres.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ lwres_printname(netdissect_options *ndo,
}
p++; /* skip terminating \0 */

return (int)(p - p0);
return ND_BYTES_BETWEEN(p, p0);

trunc:
return -1;
Expand Down Expand Up @@ -254,7 +254,7 @@ lwres_printbinlen(netdissect_options *ndo,
ND_PRINT("%02x", GET_U_1(p));
p++;
}
return (int)(p - p0);
return ND_BYTES_BETWEEN(p, p0);

trunc:
return -1;
Expand Down Expand Up @@ -297,7 +297,7 @@ lwres_printaddr(netdissect_options *ndo,
}
}

return p - p0;
return ND_BYTES_BETWEEN(p, p0);

trunc:
return -1;
Expand Down
4 changes: 2 additions & 2 deletions print-resp.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,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 = (bp_ptr - bp);
len = ND_BYTES_BETWEEN(bp_ptr, bp);
RESP_PRINT_SEGMENT(ndo, bp, len);
ret_len = 1 /*<opcode>*/ + len /*<string>*/ + 2 /*<CRLF>*/;

Expand Down Expand Up @@ -436,7 +436,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 = (bp_ptr - bp);
len = ND_BYTES_BETWEEN(bp_ptr, bp);
RESP_PRINT_SEGMENT(ndo, bp, len);

/*
Expand Down
12 changes: 6 additions & 6 deletions print-smb.c
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ print_negprot(netdissect_options *ndo,
smb_fdata(ndo, words + 1, f1, min(words + 1 + wct * 2, maxbuf),
unicodestr);
else
smb_data_print(ndo, words + 1, min(wct * 2, PTR_DIFF(maxbuf, words + 1)));
smb_data_print(ndo, words + 1, min(wct * 2, ND_BYTES_BETWEEN(maxbuf, words + 1)));

ND_TCHECK_2(data);
bcc = GET_LE_U_2(data);
Expand All @@ -430,7 +430,7 @@ print_negprot(netdissect_options *ndo,
maxbuf), unicodestr);
else
smb_data_print(ndo, data + 2,
min(GET_LE_U_2(data), PTR_DIFF(maxbuf, data + 2)));
min(GET_LE_U_2(data), ND_BYTES_BETWEEN(maxbuf, data + 2)));
}
return;
trunc:
Expand Down Expand Up @@ -464,7 +464,7 @@ print_sesssetup(netdissect_options *ndo,
smb_fdata(ndo, words + 1, f1, min(words + 1 + wct * 2, maxbuf),
unicodestr);
else
smb_data_print(ndo, words + 1, min(wct * 2, PTR_DIFF(maxbuf, words + 1)));
smb_data_print(ndo, words + 1, min(wct * 2, ND_BYTES_BETWEEN(maxbuf, words + 1)));

ND_TCHECK_2(data);
bcc = GET_LE_U_2(data);
Expand All @@ -475,7 +475,7 @@ print_sesssetup(netdissect_options *ndo,
maxbuf), unicodestr);
else
smb_data_print(ndo, data + 2,
min(GET_LE_U_2(data), PTR_DIFF(maxbuf, data + 2)));
min(GET_LE_U_2(data), ND_BYTES_BETWEEN(maxbuf, data + 2)));
}
return;
trunc:
Expand Down Expand Up @@ -516,7 +516,7 @@ print_lockingandx(netdissect_options *ndo,
maxbuf), unicodestr);
else
smb_data_print(ndo, data + 2,
min(GET_LE_U_2(data), PTR_DIFF(maxbuf, data + 2)));
min(GET_LE_U_2(data), ND_BYTES_BETWEEN(maxbuf, data + 2)));
}
return;
trunc:
Expand Down Expand Up @@ -892,7 +892,7 @@ print_smb(netdissect_options *ndo,
} else {
if (bcc > 0) {
ND_PRINT("smb_buf[]=\n");
smb_data_print(ndo, data + 2, min(bcc, PTR_DIFF(maxbuf, data + 2)));
smb_data_print(ndo, data + 2, min(bcc, ND_BYTES_BETWEEN(maxbuf, data + 2)));
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions smb.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@
#define TRANSACT2_FINDNOTIFYNEXT 12
#define TRANSACT2_MKDIR 13

#define PTR_DIFF(p1, p2) ((size_t)(((const char *)(p1)) - (const char *)(p2)))

/* some protos */
const u_char *smb_fdata(netdissect_options *, const u_char *, const char *, const u_char *, int);
extern const char *smb_errstr(int, int);
Expand Down
12 changes: 6 additions & 6 deletions smbutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ name_len(netdissect_options *ndo,
s += GET_U_1(s) + 1;
ND_TCHECK_1(s);
}
return(PTR_DIFF(s, s0) + 1);
return(ND_BYTES_BETWEEN(s, s0) + 1);

trunc:
return(-1); /* name goes past the end of the buffer */
Expand Down Expand Up @@ -330,7 +330,7 @@ write_bits(netdissect_options *ndo,
u_int i = 0;

while ((p = strchr(fmt, '|'))) {
size_t l = PTR_DIFF(p, fmt);
u_int l = ND_BYTES_BETWEEN(p, fmt);
if (l && (val & (1 << i)))
ND_PRINT("%.*s ", (int)l, fmt);
fmt = p + 1;
Expand Down Expand Up @@ -464,7 +464,7 @@ smb_fdata1(netdissect_options *ndo,
u_int l;

p = strchr(++fmt, '}');
l = PTR_DIFF(p, fmt);
l = ND_BYTES_BETWEEN(p, fmt);

if (l > sizeof(bitfmt) - 1)
l = sizeof(bitfmt)-1;
Expand Down Expand Up @@ -728,7 +728,7 @@ smb_fdata1(netdissect_options *ndo,

switch (t) {
case 1:
name_type = name_extract(ndo, startbuf, PTR_DIFF(buf, startbuf),
name_type = name_extract(ndo, startbuf, ND_BYTES_BETWEEN(buf, startbuf),
maxbuf, nbuf);
if (name_type < 0)
goto trunc;
Expand Down Expand Up @@ -885,8 +885,8 @@ smb_fdata(netdissect_options *ndo,
}
}
if (!depth && buf < maxbuf) {
size_t len = PTR_DIFF(maxbuf, buf);
ND_PRINT("Data: (%lu bytes)\n", (unsigned long)len);
u_int len = ND_BYTES_BETWEEN(maxbuf, buf);
ND_PRINT("Data: (%u bytes)\n", len);
smb_data_print(ndo, buf, len);
return(buf + len);
}
Expand Down

0 comments on commit f9c2c90

Please sign in to comment.