8199110: Address Internet Addresses
authorvtewari
Fri, 30 Mar 2018 08:37:31 +0530
changeset 52157 7593b9b8e8f2
parent 52156 1801fada294a
child 52158 f351c1a6c37a
8199110: Address Internet Addresses Reviewed-by: chegar, rriggs, igerasim, skoivu, rhalade
src/java.base/share/classes/java/net/InetAddress.java
src/java.base/share/classes/java/net/NetworkInterface.java
src/java.base/unix/native/libnet/NetworkInterface.c
--- a/src/java.base/share/classes/java/net/InetAddress.java	Wed Mar 28 08:42:45 2018 -0700
+++ b/src/java.base/share/classes/java/net/InetAddress.java	Fri Mar 30 08:37:31 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1995, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -35,6 +35,7 @@
 import java.io.ObjectStreamException;
 import java.io.ObjectStreamField;
 import java.io.IOException;
+import java.io.InvalidObjectException;
 import java.io.ObjectInputStream;
 import java.io.ObjectInputStream.GetField;
 import java.io.ObjectOutputStream;
@@ -1727,8 +1728,11 @@
         }
         GetField gf = s.readFields();
         String host = (String)gf.get("hostName", null);
-        int address= gf.get("address", 0);
-        int family= gf.get("family", 0);
+        int address = gf.get("address", 0);
+        int family = gf.get("family", 0);
+        if (family != IPv4 && family != IPv6) {
+            throw new InvalidObjectException("invalid address family type: " + family);
+        }
         InetAddressHolder h = new InetAddressHolder(host, address, family);
         UNSAFE.putObject(this, FIELDS_OFFSET, h);
     }
--- a/src/java.base/share/classes/java/net/NetworkInterface.java	Wed Mar 28 08:42:45 2018 -0700
+++ b/src/java.base/share/classes/java/net/NetworkInterface.java	Fri Mar 30 08:37:31 2018 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -321,8 +321,20 @@
         if (addr == null) {
             throw new NullPointerException();
         }
-        if (!(addr instanceof Inet4Address || addr instanceof Inet6Address)) {
-            throw new IllegalArgumentException ("invalid address type");
+        if (addr instanceof Inet4Address) {
+            Inet4Address inet4Address = (Inet4Address) addr;
+            if (inet4Address.holder.family != InetAddress.IPv4) {
+                throw new IllegalArgumentException("invalid family type: "
+                        + inet4Address.holder.family);
+            }
+        } else if (addr instanceof Inet6Address) {
+            Inet6Address inet6Address = (Inet6Address) addr;
+            if (inet6Address.holder.family != InetAddress.IPv6) {
+                throw new IllegalArgumentException("invalid family type: "
+                        + inet6Address.holder.family);
+            }
+        } else {
+            throw new IllegalArgumentException("invalid address type: " + addr);
         }
         return getByInetAddress0(addr);
     }
--- a/src/java.base/unix/native/libnet/NetworkInterface.c	Wed Mar 28 08:42:45 2018 -0700
+++ b/src/java.base/unix/native/libnet/NetworkInterface.c	Fri Mar 30 08:37:31 2018 +0530
@@ -331,9 +331,16 @@
     netif *ifs, *curr;
     jobject obj = NULL;
     jboolean match = JNI_FALSE;
-    int family = (getInetAddress_family(env, iaObj) == java_net_InetAddress_IPv4) ?
-        AF_INET : AF_INET6;
+    int family = getInetAddress_family(env, iaObj);
     JNU_CHECK_EXCEPTION_RETURN(env, NULL);
+
+    if (family == java_net_InetAddress_IPv4) {
+        family = AF_INET;
+    } else if (family == java_net_InetAddress_IPv6) {
+        family = AF_INET6;
+    } else {
+        return NULL; // Invalid family
+    }
     ifs = enumInterfaces(env);
     if (ifs == NULL) {
         return NULL;