From: Guy Harris Date: Thu, 16 Feb 2017 00:11:46 +0000 (-0800) Subject: Clean up addrtostr6(). X-Git-Tag: tcpdump-4.99-bp~1945 X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/761e1c46996b388e5ab00bf5e0fe2148be163c16 Clean up addrtostr6(). "Word" in "words" means "16-bit words", or "16-bit piece of an IPv6 address". Declare it so. Instead of going over the IPv6 address a byte at a time, process 2 bytes at a time; it makes what the code's doing more obvious. Should squelch Coverity CID 1324572. --- diff --git a/addrtostr.c b/addrtostr.c index 6f6ef11a..6b067675 100644 --- a/addrtostr.c +++ b/addrtostr.c @@ -113,16 +113,15 @@ addrtostr6 (const void *src, char *dst, size_t size) int base; int len; } best, cur; - u_long words [IN6ADDRSZ / INT16SZ]; + uint16_t words [IN6ADDRSZ / INT16SZ]; int i; /* Preprocess: * Copy the input (bytewise) array into a wordwise array. * Find the longest run of 0x00's in src[] for :: shorthanding. */ - memset (words, 0, sizeof(words)); - for (i = 0; i < IN6ADDRSZ; i++) - words[i/2] |= (srcaddr[i] << ((1 - (i % 2)) << 3)); + for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) + words[i] = (srcaddr[2*i] << 8) | srcaddr[2*i + 1]; best.len = 0; best.base = -1; @@ -192,7 +191,7 @@ addrtostr6 (const void *src, char *dst, size_t size) space_left -= added_space; break; } - snprintfed = snprintf (dp, space_left, "%lx", words[i]); + snprintfed = snprintf (dp, space_left, "%x", words[i]); if (snprintfed < 0) return (NULL); if ((size_t) snprintfed >= space_left)