提交 90134bcf 编写于 作者: C chegar

6628576: InterfaceAddress.equals() NPE when broadcast field == null

Summary: Update logic in equals to correctly handle nulls.
Reviewed-by: michaelm
上级 9e17875a
...@@ -103,11 +103,9 @@ public class InterfaceAddress { ...@@ -103,11 +103,9 @@ public class InterfaceAddress {
return false; return false;
} }
InterfaceAddress cmp = (InterfaceAddress) obj; InterfaceAddress cmp = (InterfaceAddress) obj;
if ((address != null & cmp.address == null) || if ( !(address == null ? cmp.address == null : address.equals(cmp.address)) )
(!address.equals(cmp.address)))
return false; return false;
if ((broadcast != null & cmp.broadcast == null) || if ( !(broadcast == null ? cmp.broadcast == null : broadcast.equals(cmp.broadcast)) )
(!broadcast.equals(cmp.broadcast)))
return false; return false;
if (maskLength != cmp.maskLength) if (maskLength != cmp.maskLength)
return false; return false;
......
/*
* Copyright 2008 Sun Microsystems, Inc. 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/* @test
* @bug 6628576
* @summary InterfaceAddress.equals() NPE when broadcast field == null
*/
import java.net.InterfaceAddress;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
public class Equals
{
public static void main(String[] args) {
InterfaceAddress ia1;
InterfaceAddress ia2;
InetAddress loopbackAddr = InetAddress.getLoopbackAddress();
InetAddress broadcast1 = null;
InetAddress broadcast2 = null;
try {
broadcast1 = InetAddress.getByName("255.255.255.0");
broadcast2 = InetAddress.getByName("255.255.0.0");
} catch (UnknownHostException e) {
e.printStackTrace();
}
ia1 = createInterfaceAddress(loopbackAddr, (InetAddress) null, (short)45);
ia2 = createInterfaceAddress(loopbackAddr, (InetAddress) null, (short)45);
compare(ia1, ia2, true);
ia2 = createInterfaceAddress(loopbackAddr, broadcast1, (short)45);
compare(ia1, ia2, false);
ia2 = createInterfaceAddress((InetAddress)null, broadcast1, (short)45);
compare(ia1, ia2, false);
ia1 = createInterfaceAddress(loopbackAddr, broadcast2, (short)45);
ia2 = createInterfaceAddress(loopbackAddr, broadcast2, (short)45);
compare(ia1, ia2, true);
ia1.equals(null);
}
static void compare(InterfaceAddress ia1, InterfaceAddress ia2, boolean equal) {
if (ia1.equals(ia2) != equal)
throw new RuntimeException("Failed: " + ia1 + " not equals to " + ia2);
if (ia2.equals(ia1) != equal)
throw new RuntimeException("Failed: " + ia2 + " not equals to " + ia1);
}
/**
* Returns an InterfaceAddress instance with its fields set the the values
* specificed.
*/
static InterfaceAddress createInterfaceAddress(
InetAddress address, InetAddress broadcast, short prefixlength) {
try {
Class<InterfaceAddress> IAClass = InterfaceAddress.class;
InterfaceAddress ia;
Constructor<InterfaceAddress> ctr = IAClass.getDeclaredConstructor();
ctr.setAccessible(true);
Field addressField = IAClass.getDeclaredField("address");
addressField.setAccessible(true);
Field broadcastField = IAClass.getDeclaredField("broadcast");
broadcastField.setAccessible(true);
Field maskLengthField = IAClass.getDeclaredField("maskLength");
maskLengthField.setAccessible(true);
ia = ctr.newInstance();
addressField.set(ia, address);
broadcastField.set(ia, broadcast);
maskLengthField.setShort(ia, prefixlength);
return ia;
} catch (NoSuchFieldException nsfe) {
nsfe.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InstantiationException ie) {
ie.printStackTrace();
} catch (IllegalAccessException iae) {
iae.printStackTrace();
} catch (InvocationTargetException ite) {
ite.printStackTrace();
}
return null;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册