InetAddress.getByName/getAllByName for empty host string

Chris Hegarty chris.hegarty at oracle.com
Fri Apr 13 15:11:14 UTC 2018


Hi Jaikiran,

On 13/04/18 15:29, Jaikiran Pai wrote:
> The javadoc of InetAddress.getByName(host) and getAllByName(host) states 
> that:
> 
> If the host is null then an InetAddress representing an address of the 
> loopback interface is returned.
> 
> For non-null values the javadoc explains what the implementation does. 
> However, there seems to be no mention of what it does with an empty 
> string (length == 0) value for the host param. Right now, the 
> implementation seems to treat an empty value the same way it treats the 
> host == null case and returns an InetAddress representing the loopback 
> address.
> 
> Consider the following example:
> 
> public class InetAddressTest {
>      public static void main(final String[] args) throws Exception {
>          System.out.println("InetAddress.getByName() for empty string 
> returns " + java.net.InetAddress.getByName(""));
>          System.out.println("InetAddress.getAllByName() for empty string 
> returns "
>              + 
> java.util.Arrays.toString(java.net.InetAddress.getAllByName("")));
> 
>      }
> }
> 
> This outputs:
> 
> InetAddress.getByName() for empty string returns localhost/127.0.0.1
> InetAddress.getAllByName() for empty string returns [localhost/127.0.0.1]
> 
> 
> Is it intentional for these APIs to behave this way for empty string?

Yes.

> If 
> so, should the javadoc be updated to explicitly state this behaviour?

Yeah, probably.

The following JIRA issue has been filed to track this:
   https://bugs.openjdk.java.net/browse/JDK-8201545

-Chris.


More information about the core-libs-dev mailing list