7024560: InetAddress.getLocalHost().getHostName() returns localhost for hostnames of length HOST_NAME_MAX
authorchegar
Fri, 11 Mar 2011 08:47:10 +0000
changeset 8774 4403ce77f306
parent 8773 d8a2b71afa0c
child 8775 5bcd5ec71e27
7024560: InetAddress.getLocalHost().getHostName() returns localhost for hostnames of length HOST_NAME_MAX Reviewed-by: alanb, michaelm
jdk/src/solaris/native/java/net/Inet4AddressImpl.c
jdk/src/solaris/native/java/net/Inet6AddressImpl.c
--- a/jdk/src/solaris/native/java/net/Inet4AddressImpl.c	Thu Mar 10 18:21:24 2011 +0000
+++ b/jdk/src/solaris/native/java/net/Inet4AddressImpl.c	Fri Mar 11 08:47:10 2011 +0000
@@ -60,7 +60,7 @@
     char hostname[MAXHOSTNAMELEN+1];
 
     hostname[0] = '\0';
-    if (JVM_GetHostName(hostname, MAXHOSTNAMELEN)) {
+    if (JVM_GetHostName(hostname, sizeof(hostname))) {
         /* Something went wrong, maybe networking is not setup? */
         strcpy(hostname, "localhost");
     } else {
@@ -83,6 +83,9 @@
         char *buf2[HENT_BUF_SIZE/(sizeof (char *))];
         int h_error=0;
 
+        // ensure null-terminated
+        hostname[MAXHOSTNAMELEN] = '\0';
+
 #ifdef __GLIBC__
         gethostbyname_r(hostname, &res, (char*)buf, sizeof(buf), &hp, &h_error);
 #else
--- a/jdk/src/solaris/native/java/net/Inet6AddressImpl.c	Thu Mar 10 18:21:24 2011 +0000
+++ b/jdk/src/solaris/native/java/net/Inet6AddressImpl.c	Fri Mar 11 08:47:10 2011 +0000
@@ -64,10 +64,12 @@
     char hostname[NI_MAXHOST+1];
 
     hostname[0] = '\0';
-    if (JVM_GetHostName(hostname, MAXHOSTNAMELEN)) {
+    if (JVM_GetHostName(hostname, sizeof(hostname))) {
         /* Something went wrong, maybe networking is not setup? */
         strcpy(hostname, "localhost");
     } else {
+        // ensure null-terminated
+        hostname[NI_MAXHOST] = '\0';
 #ifdef __linux__
         /* On Linux gethostname() says "host.domain.sun.com".  On
          * Solaris gethostname() says "host", so extra work is needed.