]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Author: Alex Rousskov <rousskov@measurement-factory.com>
authorAmos Jeffries <squid3@treenet.co.nz>
Wed, 26 May 2010 03:46:04 +0000 (15:46 +1200)
committerAmos Jeffries <squid3@treenet.co.nz>
Wed, 26 May 2010 03:46:04 +0000 (15:46 +1200)
Author: Amos Jeffries <squid3@treenet.co.nz>
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

index 0e5ff9f00196fbbe71c0d1011bfb799cca6b17d5..14a083cd1ee70a1185c345515c218eba5d05480e 100644 (file)
@@ -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