]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Clean up addrtostr6().
authorGuy Harris <[email protected]>
Thu, 16 Feb 2017 00:11:46 +0000 (16:11 -0800)
committerDenis Ovsienko <[email protected]>
Wed, 13 Sep 2017 11:25:44 +0000 (12:25 +0100)
"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.

addrtostr.c

index 6f6ef11a5d168f449e44615fdc67c8ab0607270a..6b067675944f70832f3c3647a5a89ca10136bd61 100644 (file)
@@ -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)