]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Maintenance: Better inet_ntop6() local variable declarations (#1080)
authorgkinkie@gmail.com <kinkie@squid-cache.org>
Sat, 4 Mar 2023 11:08:04 +0000 (11:08 +0000)
committerSquid Anubis <squid-anubis@squid-cache.org>
Sat, 4 Mar 2023 13:53:37 +0000 (13:53 +0000)
compat/inet_ntop.cc

index 09e36d58a32d2e04d0a518d010951df7659afa07..1bf2c9d222234f7337ca405267da966f8de3bd0c 100644 (file)
@@ -150,7 +150,7 @@ inet_ntop4(const u_char *src, char *dst, size_t size)
 /* const char *
  * inet_ntop6(src, dst, size)
  *  convert IPv6 binary address into presentation (printable) format
- * author:
+ * original author:
  *  Paul Vixie, 1996.
  */
 static const char *
@@ -163,24 +163,20 @@ inet_ntop6(const u_char *src, char *dst, size_t size)
      * Keep this in mind if you think this function should have been coded
      * to use pointer overlays.  All the world's not a VAX.
      */
-    char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
-    struct { int base, len; } best, cur;
-    u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
-    int i;
+    struct  {
+        int base = -1;
+        int len = 0;
+    } best, cur;
+    unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ] = {};
 
     /*
      * 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 < NS_IN6ADDRSZ; i++)
+    for (int i = 0; i < NS_IN6ADDRSZ; ++i)
         words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
-    best.base = -1;
-    best.len = 0;
-    cur.base = -1;
-    cur.len = 0;
-    for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
+    for (int i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); ++i) {
         if (words[i] == 0) {
             if (cur.base == -1)
                 cur.base = i, cur.len = 1;
@@ -204,8 +200,11 @@ inet_ntop6(const u_char *src, char *dst, size_t size)
     /*
      * Format the result.
      */
-    tp = tmp;
-    for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
+    const auto bufSize =
+        sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255");
+    char buf[bufSize];
+    char *tp = buf;
+    for (int i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); ++i) {
         /* Are we inside the best run of 0x00's? */
         if (best.base != -1 && i >= best.base &&
                 i < (best.base + best.len)) {
@@ -220,12 +219,12 @@ inet_ntop6(const u_char *src, char *dst, size_t size)
         if (i == 6 && best.base == 0 && (best.len == 6 ||
                                          (best.len == 7 && words[7] != 0x0001) ||
                                          (best.len == 5 && words[5] == 0xffff))) {
-            if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
+            if (!inet_ntop4(src+12, tp, sizeof buf - (tp - buf)))
                 return nullptr;
             tp += strlen(tp);
             break;
         }
-        tp += snprintf(tp, (tmp + sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255") - tp), "%x", words[i]);
+        tp += snprintf(tp, (buf + bufSize - tp), "%x", words[i]);
     }
     /* Was it a trailing run of 0x00's? */
     if (best.base != -1 && (best.base + best.len) ==
@@ -236,11 +235,11 @@ inet_ntop6(const u_char *src, char *dst, size_t size)
     /*
      * Check for overflow, copy, and we're done.
      */
-    if ((size_t)(tp - tmp) > size) {
+    if ((size_t)(tp - buf) > size) {
         errno = ENOSPC;
         return nullptr;
     }
-    strcpy(dst, tmp);
+    strcpy(dst, buf);
     return (dst);
 }