changeset 3745:ab13f19ee0ff

7024560: InetAddress.getLocalHost().getHostName() returns localhost for hostnames of length HOST_NAME_MAX Reviewed-by: alanb, michaelm
author chegar
date Fri, 11 Mar 2011 08:47:10 +0000
parents 7766633ec80f
children 107d55ecd50d
files src/solaris/native/java/net/Inet4AddressImpl.c src/solaris/native/java/net/Inet6AddressImpl.c
diffstat 2 files changed, 7 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/solaris/native/java/net/Inet4AddressImpl.c	Thu Mar 10 18:21:24 2011 +0000
+++ b/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/src/solaris/native/java/net/Inet6AddressImpl.c	Thu Mar 10 18:21:24 2011 +0000
+++ b/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.