From e3f9ca785ca2b39a241b57aa345547ab885d58c3 Mon Sep 17 00:00:00 2001 From: Amos Jeffries Date: Wed, 26 May 2010 15:46:04 +1200 Subject: [PATCH] Author: Alex Rousskov Author: Amos Jeffries Fixed IpAddress port printing for ports higher than 9999: snprintf includes zero-terminator in its size limit, so 7 rather than 6 bytes are needed to snprintf a colon followed by 5 port digits. Also, fix ToHostname calculation potentially truncating port numbers --- src/ip/IpAddress.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ip/IpAddress.cc b/src/ip/IpAddress.cc index 0e5ff9f001..14a083cd1e 100644 --- a/src/ip/IpAddress.cc +++ b/src/ip/IpAddress.cc @@ -978,11 +978,11 @@ unsigned int IpAddress::ToHostname(char *buf, const unsigned int blen) const p++; } - /* 7 being space for [,], and port */ + /* 8 being space for [ ] : and port digits */ if ( IsIPv6() ) - NtoA(p, blen-7, AF_INET6); + NtoA(p, blen-8, AF_INET6); else - NtoA(p, blen-7, AF_INET); + NtoA(p, blen-8, AF_INET); // find the end of the new string while (*p != '\0' && p < buf+blen) @@ -1012,9 +1012,9 @@ char* IpAddress::ToURL(char* buf, unsigned int blen) const p += ToHostname(p, blen); - if (m_SocketAddr.sin6_port > 0 && p < (buf+blen-6) ) { - /* 6 is max length of expected ':port' (short int) */ - snprintf(p, 6,":%d", GetPort() ); + if (m_SocketAddr.sin6_port > 0 && p <= (buf+blen-7) ) { + // ':port' (short int) needs at most 6 bytes plus 1 for 0-terminator + snprintf(p, 7, ":%d", GetPort() ); } // force a null-terminated string -- 2.47.3