]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Clean up rounding up.
authorGuy Harris <[email protected]>
Mon, 11 Nov 2019 04:51:48 +0000 (20:51 -0800)
committerGuy Harris <[email protected]>
Mon, 11 Nov 2019 04:51:48 +0000 (20:51 -0800)
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.

netdissect.h
print-forces.c
print-hncp.c
print-nfs.c
print-wb.c

index 7267cff851482d1a26e2cded5264624c9599df38..209246e830cbe624db892fe258f167da08d9fd31 100644 (file)
@@ -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 <stdarg.h>
index 9a3229dd8b289564e8f13fb22acef19460ff54f8..425af7e94353d4b27c937e2488b876aed2a611ee 100644 (file)
@@ -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)))
index 0593b65b9d2795e15dc43eb821de5efb9dd84a0b..06c8c1a563e69835f8a5c0cf3b5d0016aadb6c2a 100644 (file)
@@ -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);
 
index e660a706168c91eae3b8eba3f0cdc0ab1843f08c..d493cadec4056694646a8df3b5e619ce56c4d5a3 100644 (file)
@@ -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("\"");
index f9863cbf514044076223586c277835163502a6d8..24520e67796d42a8fde267851f3a1baacfe12196 100644 (file)
@@ -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)))))