JDK-7024560 : InetAddress.getLocalHost().getHostName() returns localhost for hostnames of length HOST_NAME_MAX
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2011-03-04
  • Updated: 2014-09-30
  • Resolved: 2011-04-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b136Fixed
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_07"



ADDITIONAL OS VERSION INFORMATION :
Linux cc.server 2.6.11-uni #1 Sat Jan 29 07:47:43 GMT 2011 i686 i686 i386 GNU/Linux



A DESCRIPTION OF THE PROBLEM :
When I have my hostname set to max length, InetAddress.getLocalHost().getCanonicalHostname() always returns 'localhost'.

If I have hostname less than maximum allowed characters, it works fine.

In my system, maximum hostname length is 64 chars.  (I got this from getconf HOST_NAME_MAX)

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1) set your hostname to a string which has less than HOST_NAME_MAX chars (run command 'hostname <string>')
2) Run Java program to print out InetAddress.getLocalHost().getHostName()
3) Verify that output is correct, it prints out whatever you set as hostname
4) set hostname to a string with exactly HOST_NAME_MAX length (by running hostname command)
5) Run Java program to print out InetAddress.getLocalHost().getHostName()
6) Now program prints out just 'localhost' and not what you set by running hostname command

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
InetAddress.getLocalHost().getHostName() should return hostname even when hostname is of max length
ACTUAL -
InetAddress.getLocalHost().getHostName() returns just localhost when hostname is of max length

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class Test {
    public static void main(String s[]) throws Exception {
        System.out.println(InetAddress.getLocalHost().getHostName());
    }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Work around is to always set hostname to less than max allowed length.

Comments
EVALUATION Also, we explicitly add the null-terminator to the last element in the buffer since the gethostname API would appear to allow implementations to return non null-terminated strings in the case where the name is too large to fit in the given buffer. JDK7 changeset: Changeset: ab13f19ee0ff Author: chegar Date: 2011-03-11 08:47 +0000 URL: http://hg.openjdk.java.net/jdk7/tl/jdk/rev/ab13f19ee0ff 7024560: InetAddress.getLocalHost().getHostName() returns localhost for hostnames of length HOST_NAME_MAX Reviewed-by: alanb, michaelm ! src/solaris/native/java/net/Inet4AddressImpl.c ! src/solaris/native/java/net/Inet6AddressImpl.c
11-03-2011

EVALUATION In Java_java_net_Inet(4|6)AddressImpl_getLocalHostName JVM_GetHostName, a.k.a gethostname, is returning ENAMETOOLONG. Then falling back to use "localhost", the assumtion was that failure would only happen when something went wrong, maybe networking is not setup. It appears that on many Linux variations the given buffer length should include space for the null terminator. Our given buffers already account for this, just need to pass down the correct length to gethostname.
09-03-2011