8194676: NullPointerException is thrown if ipaddress is not set

Reviewed-by: chegar, rriggs
This commit is contained in:
Vyom Tewari 2018-01-25 16:22:52 +05:30
parent 7e5577a6c1
commit 78de84bc1e
2 changed files with 97 additions and 2 deletions

View File

@ -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
@ -595,7 +595,7 @@ class Inet6Address extends InetAddress {
}
ObjectInputStream.GetField gf = s.readFields();
byte[] ipaddress = (byte[])gf.get("ipaddress", null);
byte[] ipaddress = (byte[])gf.get("ipaddress", new byte[0]);
int scope_id = gf.get("scope_id", -1);
boolean scope_id_set = gf.get("scope_id_set", false);
boolean scope_ifname_set = gf.get("scope_ifname_set", false);

View File

@ -0,0 +1,95 @@
/*
* Copyright (c) 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInputStream;
import java.io.ObjectStreamConstants;
import static java.io.ObjectStreamConstants.STREAM_MAGIC;
import static java.io.ObjectStreamConstants.TC_CLASSDESC;
import static java.io.ObjectStreamConstants.TC_ENDBLOCKDATA;
import static java.io.ObjectStreamConstants.TC_NULL;
import static java.io.ObjectStreamConstants.TC_OBJECT;
import java.net.Inet6Address;
/**
* @test
* @bug 8194676
* @summary NullPointerException is thrown if ipaddress is not set.
* @run main Inet6AddressSerTest
*/
public class Inet6AddressSerTest implements ObjectStreamConstants {
static class PayloadTest {
private static byte[] serialize(String className) throws IOException {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos)) {
// stream headers
dos.writeShort(STREAM_MAGIC);
dos.writeShort(5); // version
// Inet6Address
dos.writeByte(TC_OBJECT);
dos.writeByte(TC_CLASSDESC);
dos.writeUTF(className);
dos.writeLong(6880410070516793377L);
dos.writeByte(2);
dos.writeShort(0);
dos.writeByte(TC_ENDBLOCKDATA);
dos.writeByte(TC_NULL);
return baos.toByteArray();
}
}
private static Object deserialize(final byte[] buffer)
throws IOException, ClassNotFoundException {
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(buffer))) {
return ois.readObject();
}
}
void test(String className) throws IOException, ClassNotFoundException {
deserialize(serialize(className));
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
try {
new PayloadTest().test(Inet6Address.class.getName());
throw new RuntimeException("Expected exception not raised");
} catch (InvalidObjectException ioe) {
if (ioe.getMessage().contains("invalid address length")) {
System.out.println(String.format("Got expected exception: %s", ioe));
} else {
throw new RuntimeException("Expected exception not raised");
}
} catch (RuntimeException re) {
throw new RuntimeException("Expected exception not raised");
}
}
}