This is a serialization issue in Inet6Address. When de-serializing an address with an interface name as the scope, the test for its existence should be done sooner rather than later to avoid a NullPointerException. I took the opportunity to clean up the code in Inet6Address to act on some NetBeans warning, in particular re-named a local variable that was masking a field.

See patch attached.

--- /home/jccollet/tmp/webrev/raw_files/old/src/share/classes/java/net/Inet6Address.java	2008-06-16 17:14:55.000000000 +0200
+++ /home/jccollet/tmp/webrev/raw_files/new/src/share/classes/java/net/Inet6Address.java	2008-06-16 17:14:55.000000000 +0200
@@ -25,12 +25,9 @@
 
 package java.net;
 
-import java.security.AccessController;
 import java.io.ObjectInputStream;
 import java.io.IOException;
-import java.io.ObjectStreamException;
 import java.io.InvalidObjectException;
-import sun.security.action.*;
 import java.util.Enumeration;
 
 /**
@@ -360,11 +357,11 @@
     private int deriveNumericScope (NetworkInterface ifc) throws UnknownHostException {
         Enumeration addresses = ifc.getInetAddresses();
         while (addresses.hasMoreElements()) {
-            InetAddress address = (InetAddress)addresses.nextElement();
-            if (!(address instanceof Inet6Address)) {
+            InetAddress addr = (InetAddress)addresses.nextElement();
+            if (!(addr instanceof Inet6Address)) {
                 continue;
             }
-            Inet6Address ia6_addr = (Inet6Address)address;
+            Inet6Address ia6_addr = (Inet6Address)addr;
             /* check if site or link local prefixes match */
             if (!differentLocalAddressTypes(ia6_addr)){
                 /* type not the same, so carry on searching */
@@ -388,11 +385,11 @@
             if (ifc.getName().equals (ifname)) {
                 Enumeration addresses = ifc.getInetAddresses();
                 while (addresses.hasMoreElements()) {
-                    InetAddress address = (InetAddress)addresses.nextElement();
-                    if (!(address instanceof Inet6Address)) {
+                    InetAddress addr = (InetAddress)addresses.nextElement();
+                    if (!(addr instanceof Inet6Address)) {
                         continue;
                     }
-                    Inet6Address ia6_addr = (Inet6Address)address;
+                    Inet6Address ia6_addr = (Inet6Address)addr;
                     /* check if site or link local prefixes match */
                     if (!differentLocalAddressTypes(ia6_addr)){
                         /* type not the same, so carry on searching */
@@ -420,21 +417,22 @@
         if (ifname != null && !"".equals (ifname)) {
             try {
                 scope_ifname = NetworkInterface.getByName(ifname);
-                try {
-                    scope_id = deriveNumericScope (scope_ifname);
-                } catch (UnknownHostException e) {
-                    // should not happen
-                    assert false;
+                if (scope_ifname == null) {
+                    /* the interface does not exist on this system, so we clear
+                     * the scope information completely */
+                    scope_id_set = false;
+                    scope_ifname_set = false;
+                    scope_id = 0;
+                } else {
+                    try {
+                        scope_id = deriveNumericScope (scope_ifname);
+                    } catch (UnknownHostException e) {
+                        // should not happen
+                        assert false;
+                    }
                 }
             } catch (SocketException e) {}
 
-            if (scope_ifname == null) {
-                /* the interface does not exist on this system, so we clear
-                 * the scope information completely */
-                scope_id_set = false;
-                scope_ifname_set = false;
-                scope_id = 0;
-            }
         }
         /* if ifname was not supplied, then the numeric info is used */
 
@@ -460,6 +458,7 @@
      * an IP multicast address
      * @since JDK1.1
      */
+    @Override
     public boolean isMulticastAddress() {
         return ((ipaddress[0] & 0xff) == 0xff);
     }
@@ -470,6 +469,7 @@
      *         a wildcard address.
      * @since 1.4
      */
+    @Override
     public boolean isAnyLocalAddress() {
         byte test = 0x00;
         for (int i = 0; i < INADDRSZ; i++) {
@@ -485,6 +485,7 @@
      * a loopback address; or false otherwise.
      * @since 1.4
      */
+    @Override
     public boolean isLoopbackAddress() {
         byte test = 0x00;
         for (int i = 0; i < 15; i++) {
@@ -500,6 +501,7 @@
      * a link local address; or false if address is not a link local unicast address.
      * @since 1.4
      */
+    @Override
     public boolean isLinkLocalAddress() {
         return ((ipaddress[0] & 0xff) == 0xfe
                 && (ipaddress[1] & 0xc0) == 0x80);
@@ -512,6 +514,7 @@
      * a site local address; or false if address is not a site local unicast address.
      * @since 1.4
      */
+    @Override
     public boolean isSiteLocalAddress() {
         return ((ipaddress[0] & 0xff) == 0xfe
                 && (ipaddress[1] & 0xc0) == 0xc0);
@@ -525,6 +528,7 @@
      *         of global scope or it is not a multicast address
      * @since 1.4
      */
+    @Override
     public boolean isMCGlobal() {
         return ((ipaddress[0] & 0xff) == 0xff
                 && (ipaddress[1] & 0x0f) == 0x0e);
@@ -538,6 +542,7 @@
      *         of node-local scope or it is not a multicast address
      * @since 1.4
      */
+    @Override
     public boolean isMCNodeLocal() {
         return ((ipaddress[0] & 0xff) == 0xff
                 && (ipaddress[1] & 0x0f) == 0x01);
@@ -551,6 +556,7 @@
      *         of link-local scope or it is not a multicast address
      * @since 1.4
      */
+    @Override
     public boolean isMCLinkLocal() {
         return ((ipaddress[0] & 0xff) == 0xff
                 && (ipaddress[1] & 0x0f) == 0x02);
@@ -564,6 +570,7 @@
      *         of site-local scope or it is not a multicast address
      * @since 1.4
      */
+    @Override
     public boolean isMCSiteLocal() {
         return ((ipaddress[0] & 0xff) == 0xff
                 && (ipaddress[1] & 0x0f) == 0x05);
@@ -578,6 +585,7 @@
      *         or it is not a multicast address
      * @since 1.4
      */
+    @Override
     public boolean isMCOrgLocal() {
         return ((ipaddress[0] & 0xff) == 0xff
                 && (ipaddress[1] & 0x0f) == 0x08);
@@ -590,6 +598,7 @@
      *
      * @return  the raw IP address of this object.
      */
+    @Override
     public byte[] getAddress() {
         return ipaddress.clone();
     }
@@ -624,6 +633,7 @@
      *
      * @return  the raw IP address in a string format.
      */
+    @Override
     public String getHostAddress() {
         String s = numericToTextFormat(ipaddress);
         if (scope_ifname_set) { /* must check this first */
@@ -639,6 +649,7 @@
      *
      * @return  a hash code value for this IP address.
      */
+    @Override
     public int hashCode() {
         if (ipaddress != null) {
 
@@ -677,6 +688,7 @@
      *          <code>false</code> otherwise.
      * @see     java.net.InetAddress#getAddress()
      */
+    @Override
     public boolean equals(Object obj) {
         if (obj == null ||
             !(obj instanceof Inet6Address))

--- /home/jccollet/tmp/webrev/raw_files/old/test/java/net/Inet6Address/serialize/Serialize.java	2008-06-16 17:14:56.000000000 +0200
+++ /home/jccollet/tmp/webrev/raw_files/new/test/java/net/Inet6Address/serialize/Serialize.java	2008-06-16 17:14:56.000000000 +0200
@@ -24,7 +24,9 @@
 /**
  * @test
  * @bug 4921029
+ * @bug 6656849
  * @summary  java.net.Inet6Address fails to be serialized with IPv6 support
+ * @summary  NullPointerException thrown while de-serializing IPV6 Address.
  */
 
 import java.net.*;
@@ -76,11 +78,20 @@
 
          System.out.println(nobj);
 
-        // create an address with an unlikely numeric scope_id
-        if (!test ((Inet6Address)InetAddress.getByName ("fe80::1%99"))) {
-            throw new RuntimeException ("test failed on fe80::1%99");
-        }
-
+         // create an address with an unlikely numeric scope_id
+         if (!test ((Inet6Address)InetAddress.getByName ("fe80::1%99"))) {
+             throw new RuntimeException ("test failed on fe80::1%99");
+         }
+         
+         // Deserialize an Inet6 address with a named interface
+         file = new File (System.getProperty("test.src"), "serial-bge0.ser");
+         ois = new ObjectInputStream(new FileInputStream(file));
+         try {
+             nobj = (Inet6Address) ois.readObject();
+         } catch (NullPointerException e) {
+             throw new RuntimeException("6656849 Not fixed: NullPointer when deserializing");
+         }
+         System.out.println(nobj);
          System.out.println("All tests passed");
      }
 

Binary files new/test/java/net/Inet6Address/serialize/serial-bge0.ser and /dev/null differ

Reply via email to