# HG changeset patch # User vtewari # Date 1516877572 -19800 # Node ID d626620a184432a103b5e1111a1049bed8820af4 # Parent 257d7610663fba12453933a9884cf63778259482 8194676: NullPointerException is thrown if ipaddress is not set. Reviewed-by: chegar, rriggs diff -r 257d7610663f -r d626620a1844 src/java.base/share/classes/java/net/Inet6Address.java --- a/src/java.base/share/classes/java/net/Inet6Address.java Wed Jan 24 23:01:57 2018 -0800 +++ b/src/java.base/share/classes/java/net/Inet6Address.java Thu Jan 25 16:22:52 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 @@ -595,7 +595,7 @@ } 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); diff -r 257d7610663f -r d626620a1844 test/jdk/java/net/Inet6Address/serialize/Inet6AddressSerTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test/jdk/java/net/Inet6Address/serialize/Inet6AddressSerTest.java Thu Jan 25 16:22:52 2018 +0530 @@ -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"); + } + } +}