char *ipaddr_to_asc(unsigned char *p, int len)
{
+ /*
+ * 40 is enough space for the longest IPv6 address + nul terminator byte
+ * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX\0
+ */
char buf[40], *out;
+ int i = 0, remain = 0, bytes = 0;
switch (len) {
case 4: /* IPv4 */
break;
/* TODO possibly combine with static i2r_address() in v3_addr.c */
case 16: /* IPv6 */
- for (out = buf; out < buf + 8 * 3; out += 3) {
- BIO_snprintf(out, 3 + 1, "%X:", p[0] << 8 | p[1]);
+ for (out = buf, i = 8, remain = sizeof(buf);
+ i-- > 0 && bytes >= 0;
+ remain -= bytes, out += bytes) {
+ const char *template = (i > 0 ? "%X:" : "%X");
+
+ bytes = BIO_snprintf(out, remain, template, p[0] << 8 | p[1]);
p += 2;
}
- out[-1] = '\0';
break;
default:
BIO_snprintf(buf, sizeof(buf), "<invalid length=%d>", len);