[BUG] Inet6Address.isIPv4CompatibleAddress uses wrong prefix

Rob Spoor openjdk at icemanx.nl
Mon Jun 24 20:57:50 UTC 2019


I found a bug in Inet6Adress.isIPv4CompatibleAddress(). While parsing 
correctly uses the ::ffff:<IPv4> format, isIPv4CompatibleAddress() 
checks for ::<IPv4> instead. An example:

     Inet6Address address = (Inet6Address) 
InetAddress.getByName("::192.168.1.13");
     System.out.printf("%s: %b%n", address, 
address.isIPv4CompatibleAddress());

This should print false, but instead it prints true.

The error is in the Inet6Address.Inet6AddressHolder class:

         boolean isIPv4CompatibleAddress() {
             if ((ipaddress[0] == 0x00) && (ipaddress[1] == 0x00) &&
                 (ipaddress[2] == 0x00) && (ipaddress[3] == 0x00) &&
                 (ipaddress[4] == 0x00) && (ipaddress[5] == 0x00) &&
                 (ipaddress[6] == 0x00) && (ipaddress[7] == 0x00) &&
                 (ipaddress[8] == 0x00) && (ipaddress[9] == 0x00) &&
                 (ipaddress[10] == 0x00) && (ipaddress[11] == 0x00))  {
                 return true;
             }
             return false;
         }

I think that bytes 10 and 11 should both be (byte) 0xFF instead of 0x00. 
This is what's being used in IPAddressUtil, which is used for parsing:

     private static boolean isIPv4MappedAddress(byte[] addr) {
         if (addr.length < INADDR16SZ) {
             return false;
         }
         if ((addr[0] == 0x00) && (addr[1] == 0x00) &&
             (addr[2] == 0x00) && (addr[3] == 0x00) &&
             (addr[4] == 0x00) && (addr[5] == 0x00) &&
             (addr[6] == 0x00) && (addr[7] == 0x00) &&
             (addr[8] == 0x00) && (addr[9] == 0x00) &&
             (addr[10] == (byte)0xff) &&
             (addr[11] == (byte)0xff))  {
             return true;
         }
         return false;
     }

Maybe it's an idea to let Inet6Address.Inet6AddressHolder delegate to 
this latter method?


Rob


More information about the core-libs-dev mailing list