From: Guy Harris Date: Mon, 11 Nov 2019 04:51:48 +0000 (-0800) Subject: Clean up rounding up. X-Git-Tag: tcpdump-4.99-bp~602 X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/866358cc52760b68947ae57b4f75f1e1709700b6 Clean up rounding up. Have roundup2() cast the power-of-2 argument to u_int; that way, you don't have to explicitly define it as an unsigned value in order to avoid compiler or UBSan complaints about signed integers. Use it instead of rolling our own rounding-to-a-power-of-2. --- diff --git a/netdissect.h b/netdissect.h index 7267cff8..209246e8 100644 --- a/netdissect.h +++ b/netdissect.h @@ -109,7 +109,7 @@ typedef unsigned char nd_byte; * Round up x to a multiple of y; y must be a power of 2. */ #ifndef roundup2 -#define roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) +#define roundup2(x, y) (((x)+((u_int)((y)-1)))&(~((u_int)((y)-1)))) #endif #include diff --git a/print-forces.c b/print-forces.c index 9a3229dd..425af7e9 100644 --- a/print-forces.c +++ b/print-forces.c @@ -388,7 +388,7 @@ struct forces_tlv { nd_uint16_t length; }; -#define F_ALN_LEN(len) ( ((len)+ForCES_ALNL-1) & ~(ForCES_ALNL-1) ) +#define F_ALN_LEN(len) roundup2(len, ForCES_ALNL) #define GET_TOP_TLV(fhdr) ((const struct forces_tlv *)((fhdr) + sizeof (struct forcesh))) #define TLV_SET_LEN(len) (F_ALN_LEN(TLV_HDRL) + (len)) #define TLV_DATA(tlvp) ((const void*)(((const char*)(tlvp)) + TLV_SET_LEN(0))) diff --git a/print-hncp.c b/print-hncp.c index 0593b65b..06c8c1a5 100644 --- a/print-hncp.c +++ b/print-hncp.c @@ -827,7 +827,7 @@ hncp_print_rec(netdissect_options *ndo, nd_print_invalid(ndo); } l += 17; - l += -l & 3; + l = roundup2(l, 4); if (bodylen >= l) hncp_print_rec(ndo, value + l, bodylen - l, indent+1); } @@ -852,7 +852,7 @@ hncp_print_rec(netdissect_options *ndo, } skip_multiline: - i += 4 + bodylen + (-bodylen & 3); + i += 4 + roundup2(bodylen, 4); } print_type_in_line(ndo, last_type_mask, last_type_count, indent, &first_one); diff --git a/print-nfs.c b/print-nfs.c index e660a706..d493cade 100644 --- a/print-nfs.c +++ b/print-nfs.c @@ -524,7 +524,7 @@ static const uint32_t * parsefn(netdissect_options *ndo, const uint32_t *dp) { - uint32_t len; + uint32_t len, rounded_len; const u_char *cp; /* Bail if we don't have the string length */ @@ -540,11 +540,12 @@ parsefn(netdissect_options *ndo, return NULL; } - ND_TCHECK_LEN(dp, ((len + 3) & ~3)); + rounded_len = roundup2(len, 4); + ND_TCHECK_LEN(dp, rounded_len); cp = (const u_char *)dp; /* Update 32-bit pointer (NFS filenames padded to 32-bit boundaries) */ - dp += ((len + 3) & ~3) / sizeof(*dp); + dp += rounded_len / sizeof(*dp); ND_PRINT("\""); if (nd_printn(ndo, cp, len, ndo->ndo_snapend)) { ND_PRINT("\""); diff --git a/print-wb.c b/print-wb.c index f9863cbf..24520e67 100644 --- a/print-wb.c +++ b/print-wb.c @@ -46,7 +46,7 @@ * an even multiple of DOP_ALIGN bytes, which must be a power of two. */ #define DOP_ALIGN 4 -#define DOP_ROUNDUP(x) ((((int)(x)) + (DOP_ALIGN - 1)) & ~(DOP_ALIGN - 1)) +#define DOP_ROUNDUP(x) roundup2(x, DOP_ALIGN) #define DOP_NEXT(d)\ ((const struct dophdr *)((const u_char *)(d) + \ DOP_ROUNDUP(GET_BE_U_2((d)->dh_len) + sizeof(*(d)))))