From: gkinkie@gmail.com Date: Sat, 4 Mar 2023 11:08:04 +0000 (+0000) Subject: Maintenance: Better inet_ntop6() local variable declarations (#1080) X-Git-Tag: SQUID_7_0_1~468 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f4c2ca86bae89b47512a62a78d5284f332e26d40;p=thirdparty%2Fsquid.git Maintenance: Better inet_ntop6() local variable declarations (#1080) --- diff --git a/compat/inet_ntop.cc b/compat/inet_ntop.cc index 09e36d58a3..1bf2c9d222 100644 --- a/compat/inet_ntop.cc +++ b/compat/inet_ntop.cc @@ -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); }