6656849: NullPointerException thrown while de-serializing IPV6 Address
Check for existence of interface name earlier in code Reviewed-by: michaelm
This commit is contained in:
parent
19fc7593d1
commit
3fce795f6c
@ -25,12 +25,9 @@
|
|||||||
|
|
||||||
package java.net;
|
package java.net;
|
||||||
|
|
||||||
import java.security.AccessController;
|
|
||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.ObjectStreamException;
|
|
||||||
import java.io.InvalidObjectException;
|
import java.io.InvalidObjectException;
|
||||||
import sun.security.action.*;
|
|
||||||
import java.util.Enumeration;
|
import java.util.Enumeration;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -358,13 +355,13 @@ class Inet6Address extends InetAddress {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int deriveNumericScope (NetworkInterface ifc) throws UnknownHostException {
|
private int deriveNumericScope (NetworkInterface ifc) throws UnknownHostException {
|
||||||
Enumeration addresses = ifc.getInetAddresses();
|
Enumeration<InetAddress> addresses = ifc.getInetAddresses();
|
||||||
while (addresses.hasMoreElements()) {
|
while (addresses.hasMoreElements()) {
|
||||||
InetAddress address = (InetAddress)addresses.nextElement();
|
InetAddress addr = addresses.nextElement();
|
||||||
if (!(address instanceof Inet6Address)) {
|
if (!(addr instanceof Inet6Address)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Inet6Address ia6_addr = (Inet6Address)address;
|
Inet6Address ia6_addr = (Inet6Address)addr;
|
||||||
/* check if site or link local prefixes match */
|
/* check if site or link local prefixes match */
|
||||||
if (!differentLocalAddressTypes(ia6_addr)){
|
if (!differentLocalAddressTypes(ia6_addr)){
|
||||||
/* type not the same, so carry on searching */
|
/* type not the same, so carry on searching */
|
||||||
@ -377,22 +374,22 @@ class Inet6Address extends InetAddress {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int deriveNumericScope (String ifname) throws UnknownHostException {
|
private int deriveNumericScope (String ifname) throws UnknownHostException {
|
||||||
Enumeration en;
|
Enumeration<NetworkInterface> en;
|
||||||
try {
|
try {
|
||||||
en = NetworkInterface.getNetworkInterfaces();
|
en = NetworkInterface.getNetworkInterfaces();
|
||||||
} catch (SocketException e) {
|
} catch (SocketException e) {
|
||||||
throw new UnknownHostException ("could not enumerate local network interfaces");
|
throw new UnknownHostException ("could not enumerate local network interfaces");
|
||||||
}
|
}
|
||||||
while (en.hasMoreElements()) {
|
while (en.hasMoreElements()) {
|
||||||
NetworkInterface ifc = (NetworkInterface)en.nextElement();
|
NetworkInterface ifc = en.nextElement();
|
||||||
if (ifc.getName().equals (ifname)) {
|
if (ifc.getName().equals (ifname)) {
|
||||||
Enumeration addresses = ifc.getInetAddresses();
|
Enumeration addresses = ifc.getInetAddresses();
|
||||||
while (addresses.hasMoreElements()) {
|
while (addresses.hasMoreElements()) {
|
||||||
InetAddress address = (InetAddress)addresses.nextElement();
|
InetAddress addr = (InetAddress)addresses.nextElement();
|
||||||
if (!(address instanceof Inet6Address)) {
|
if (!(addr instanceof Inet6Address)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
Inet6Address ia6_addr = (Inet6Address)address;
|
Inet6Address ia6_addr = (Inet6Address)addr;
|
||||||
/* check if site or link local prefixes match */
|
/* check if site or link local prefixes match */
|
||||||
if (!differentLocalAddressTypes(ia6_addr)){
|
if (!differentLocalAddressTypes(ia6_addr)){
|
||||||
/* type not the same, so carry on searching */
|
/* type not the same, so carry on searching */
|
||||||
@ -420,21 +417,22 @@ class Inet6Address extends InetAddress {
|
|||||||
if (ifname != null && !"".equals (ifname)) {
|
if (ifname != null && !"".equals (ifname)) {
|
||||||
try {
|
try {
|
||||||
scope_ifname = NetworkInterface.getByName(ifname);
|
scope_ifname = NetworkInterface.getByName(ifname);
|
||||||
try {
|
if (scope_ifname == null) {
|
||||||
scope_id = deriveNumericScope (scope_ifname);
|
/* the interface does not exist on this system, so we clear
|
||||||
} catch (UnknownHostException e) {
|
* the scope information completely */
|
||||||
// should not happen
|
scope_id_set = false;
|
||||||
assert 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) {}
|
} 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 */
|
/* if ifname was not supplied, then the numeric info is used */
|
||||||
|
|
||||||
@ -460,6 +458,7 @@ class Inet6Address extends InetAddress {
|
|||||||
* an IP multicast address
|
* an IP multicast address
|
||||||
* @since JDK1.1
|
* @since JDK1.1
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public boolean isMulticastAddress() {
|
public boolean isMulticastAddress() {
|
||||||
return ((ipaddress[0] & 0xff) == 0xff);
|
return ((ipaddress[0] & 0xff) == 0xff);
|
||||||
}
|
}
|
||||||
@ -470,6 +469,7 @@ class Inet6Address extends InetAddress {
|
|||||||
* a wildcard address.
|
* a wildcard address.
|
||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public boolean isAnyLocalAddress() {
|
public boolean isAnyLocalAddress() {
|
||||||
byte test = 0x00;
|
byte test = 0x00;
|
||||||
for (int i = 0; i < INADDRSZ; i++) {
|
for (int i = 0; i < INADDRSZ; i++) {
|
||||||
@ -485,6 +485,7 @@ class Inet6Address extends InetAddress {
|
|||||||
* a loopback address; or false otherwise.
|
* a loopback address; or false otherwise.
|
||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public boolean isLoopbackAddress() {
|
public boolean isLoopbackAddress() {
|
||||||
byte test = 0x00;
|
byte test = 0x00;
|
||||||
for (int i = 0; i < 15; i++) {
|
for (int i = 0; i < 15; i++) {
|
||||||
@ -500,6 +501,7 @@ class Inet6Address extends InetAddress {
|
|||||||
* a link local address; or false if address is not a link local unicast address.
|
* a link local address; or false if address is not a link local unicast address.
|
||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public boolean isLinkLocalAddress() {
|
public boolean isLinkLocalAddress() {
|
||||||
return ((ipaddress[0] & 0xff) == 0xfe
|
return ((ipaddress[0] & 0xff) == 0xfe
|
||||||
&& (ipaddress[1] & 0xc0) == 0x80);
|
&& (ipaddress[1] & 0xc0) == 0x80);
|
||||||
@ -512,6 +514,7 @@ class Inet6Address extends InetAddress {
|
|||||||
* a site local address; or false if address is not a site local unicast address.
|
* a site local address; or false if address is not a site local unicast address.
|
||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public boolean isSiteLocalAddress() {
|
public boolean isSiteLocalAddress() {
|
||||||
return ((ipaddress[0] & 0xff) == 0xfe
|
return ((ipaddress[0] & 0xff) == 0xfe
|
||||||
&& (ipaddress[1] & 0xc0) == 0xc0);
|
&& (ipaddress[1] & 0xc0) == 0xc0);
|
||||||
@ -525,6 +528,7 @@ class Inet6Address extends InetAddress {
|
|||||||
* of global scope or it is not a multicast address
|
* of global scope or it is not a multicast address
|
||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public boolean isMCGlobal() {
|
public boolean isMCGlobal() {
|
||||||
return ((ipaddress[0] & 0xff) == 0xff
|
return ((ipaddress[0] & 0xff) == 0xff
|
||||||
&& (ipaddress[1] & 0x0f) == 0x0e);
|
&& (ipaddress[1] & 0x0f) == 0x0e);
|
||||||
@ -538,6 +542,7 @@ class Inet6Address extends InetAddress {
|
|||||||
* of node-local scope or it is not a multicast address
|
* of node-local scope or it is not a multicast address
|
||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public boolean isMCNodeLocal() {
|
public boolean isMCNodeLocal() {
|
||||||
return ((ipaddress[0] & 0xff) == 0xff
|
return ((ipaddress[0] & 0xff) == 0xff
|
||||||
&& (ipaddress[1] & 0x0f) == 0x01);
|
&& (ipaddress[1] & 0x0f) == 0x01);
|
||||||
@ -551,6 +556,7 @@ class Inet6Address extends InetAddress {
|
|||||||
* of link-local scope or it is not a multicast address
|
* of link-local scope or it is not a multicast address
|
||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public boolean isMCLinkLocal() {
|
public boolean isMCLinkLocal() {
|
||||||
return ((ipaddress[0] & 0xff) == 0xff
|
return ((ipaddress[0] & 0xff) == 0xff
|
||||||
&& (ipaddress[1] & 0x0f) == 0x02);
|
&& (ipaddress[1] & 0x0f) == 0x02);
|
||||||
@ -564,6 +570,7 @@ class Inet6Address extends InetAddress {
|
|||||||
* of site-local scope or it is not a multicast address
|
* of site-local scope or it is not a multicast address
|
||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public boolean isMCSiteLocal() {
|
public boolean isMCSiteLocal() {
|
||||||
return ((ipaddress[0] & 0xff) == 0xff
|
return ((ipaddress[0] & 0xff) == 0xff
|
||||||
&& (ipaddress[1] & 0x0f) == 0x05);
|
&& (ipaddress[1] & 0x0f) == 0x05);
|
||||||
@ -578,6 +585,7 @@ class Inet6Address extends InetAddress {
|
|||||||
* or it is not a multicast address
|
* or it is not a multicast address
|
||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public boolean isMCOrgLocal() {
|
public boolean isMCOrgLocal() {
|
||||||
return ((ipaddress[0] & 0xff) == 0xff
|
return ((ipaddress[0] & 0xff) == 0xff
|
||||||
&& (ipaddress[1] & 0x0f) == 0x08);
|
&& (ipaddress[1] & 0x0f) == 0x08);
|
||||||
@ -590,6 +598,7 @@ class Inet6Address extends InetAddress {
|
|||||||
*
|
*
|
||||||
* @return the raw IP address of this object.
|
* @return the raw IP address of this object.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public byte[] getAddress() {
|
public byte[] getAddress() {
|
||||||
return ipaddress.clone();
|
return ipaddress.clone();
|
||||||
}
|
}
|
||||||
@ -624,6 +633,7 @@ class Inet6Address extends InetAddress {
|
|||||||
*
|
*
|
||||||
* @return the raw IP address in a string format.
|
* @return the raw IP address in a string format.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public String getHostAddress() {
|
public String getHostAddress() {
|
||||||
String s = numericToTextFormat(ipaddress);
|
String s = numericToTextFormat(ipaddress);
|
||||||
if (scope_ifname_set) { /* must check this first */
|
if (scope_ifname_set) { /* must check this first */
|
||||||
@ -639,6 +649,7 @@ class Inet6Address extends InetAddress {
|
|||||||
*
|
*
|
||||||
* @return a hash code value for this IP address.
|
* @return a hash code value for this IP address.
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public int hashCode() {
|
public int hashCode() {
|
||||||
if (ipaddress != null) {
|
if (ipaddress != null) {
|
||||||
|
|
||||||
@ -677,6 +688,7 @@ class Inet6Address extends InetAddress {
|
|||||||
* <code>false</code> otherwise.
|
* <code>false</code> otherwise.
|
||||||
* @see java.net.InetAddress#getAddress()
|
* @see java.net.InetAddress#getAddress()
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object obj) {
|
||||||
if (obj == null ||
|
if (obj == null ||
|
||||||
!(obj instanceof Inet6Address))
|
!(obj instanceof Inet6Address))
|
||||||
|
6
jdk/test/java/net/Inet6Address/serialize/Readme.txt
Normal file
6
jdk/test/java/net/Inet6Address/serialize/Readme.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
This test uses 2 binary data files that were each created by serializing an Inet6Address instance.
|
||||||
|
In both cases this has to do with the tricky issue of scopes in serialized addresses.
|
||||||
|
|
||||||
|
serial1.4.2.ser: Was created by serializing an Inet6Address (::1) with J2SE 1.4.2 and is used to check for backward compatibility.
|
||||||
|
|
||||||
|
serial-bge0.ser: Was created on a Sparc workstation because it has an uncommon interface name ('bge0') which is useful for the test.
|
@ -24,7 +24,9 @@
|
|||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
* @bug 4921029
|
* @bug 4921029
|
||||||
|
* @bug 6656849
|
||||||
* @summary java.net.Inet6Address fails to be serialized with IPv6 support
|
* @summary java.net.Inet6Address fails to be serialized with IPv6 support
|
||||||
|
* @summary NullPointerException thrown while de-serializing IPV6 Address.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
@ -76,11 +78,20 @@ public class Serialize {
|
|||||||
|
|
||||||
System.out.println(nobj);
|
System.out.println(nobj);
|
||||||
|
|
||||||
// create an address with an unlikely numeric scope_id
|
// create an address with an unlikely numeric scope_id
|
||||||
if (!test ((Inet6Address)InetAddress.getByName ("fe80::1%99"))) {
|
if (!test ((Inet6Address)InetAddress.getByName ("fe80::1%99"))) {
|
||||||
throw new RuntimeException ("test failed on 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");
|
System.out.println("All tests passed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,8 +108,5 @@ public class Serialize {
|
|||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
BIN
jdk/test/java/net/Inet6Address/serialize/serial-bge0.ser
Normal file
BIN
jdk/test/java/net/Inet6Address/serialize/serial-bge0.ser
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user