Code Review 7024560: InetAddress.getLocalHost().getHostName() returns localhost for hostnames of length HOST_NAME_MAX

Chris Hegarty chris.hegarty at oracle.com
Wed Mar 9 08:30:18 PST 2011


On 09/03/2011 16:26, Alan Bateman wrote:
> Chris Hegarty wrote:
>> Alan, Michael,
>>
>> 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 assumption 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.
>>
>> http://cr.openjdk.java.net/~chegar/7024560/webrev.00/webrev/
>>
>> -Chris.
> Are you sure that it is failing with ENAMETOOLONG? I just checked the

I tested on a few different Linux machines and instrumented the error 
handling code, it is definitely failing with ENAMETOOLONG.

> man page and is reads "In case the null-terminated hostname does not
> fit, no error is returned, but the hostname is truncated. It is
> unspecified whether the truncated hostname will be null-terminated",
> which suggests to me that we should be careful to null terminate on the
> success path.

Yes, the man page does leave the door open for non null-terminated 
strings, but we've never seen this in the past so I didn't think it 
would be a problem in reality.

-Chris.

>
> -Alan.
>



More information about the net-dev mailing list