From: Francois-Xavier Le Bail Date: Sun, 17 Mar 2019 14:01:15 +0000 (+0100) Subject: CDP: Fix two loops for undefined behavior at runtime X-Git-Tag: tcpdump-4.99-bp~901 X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/6c93708db1cd314ab4e6511596c625d617a15853 CDP: Fix two loops for undefined behavior at runtime The errors were: print-cdp.c:363:13: runtime error: unsigned integer overflow: 0 - 1 cannot be represented in type 'u_int' (aka 'unsigned int') print-cdp.c:375:13: runtime error: unsigned integer overflow: 0 - 1 cannot be represented in type 'u_int' (aka 'unsigned int') --- diff --git a/print-cdp.c b/print-cdp.c index 79d7046c..e63ce911 100644 --- a/print-cdp.c +++ b/print-cdp.c @@ -360,9 +360,10 @@ cdp_print_addr(netdissect_options *ndo, if (p + pl > endp) goto trunc; ND_PRINT("pt=0x%02x, pl=%u, pb=", EXTRACT_U_1((p - 2)), pl); - while (pl-- > 0) { + while (pl != 0) { ND_PRINT(" %02x", EXTRACT_U_1(p)); p++; + pl--; } ND_TCHECK_2(p); if (p + 2 > endp) @@ -372,9 +373,10 @@ cdp_print_addr(netdissect_options *ndo, ND_TCHECK_LEN(p, al); if (p + al > endp) goto trunc; - while (al-- > 0) { + while (al != 0) { ND_PRINT(" %02x", EXTRACT_U_1(p)); p++; + al--; } } num--;