# HG changeset patch # User weijun # Date 1235354692 -28800 # Node ID d0bafa0668163efff729e803bd74e2cac91f4987 # Parent d690c8a2acea0e1a0120b7a922898f8ed5cb3eec 6804045: DerValue does not accept empty OCTET STRING Reviewed-by: xuelei diff -r d690c8a2acea -r d0bafa066816 jdk/src/share/classes/sun/security/util/DerValue.java --- a/jdk/src/share/classes/sun/security/util/DerValue.java Mon Feb 23 10:04:25 2009 +0800 +++ b/jdk/src/share/classes/sun/security/util/DerValue.java Mon Feb 23 10:04:52 2009 +0800 @@ -1,5 +1,5 @@ /* - * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved. + * Copyright 1996-2009 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 @@ -65,7 +65,7 @@ protected DerInputBuffer buffer; /** - * The DER-encoded data of the value. + * The DER-encoded data of the value, never null */ public final DerInputStream data; @@ -378,8 +378,6 @@ ("Indefinite length encoding not supported"); length = DerInputStream.getLength(in); } - if (length == 0) - return null; if (fullyBuffered && in.available() != length) throw new IOException("extra data given to DerValue constructor"); @@ -477,6 +475,11 @@ "DerValue.getOctetString, not an Octet String: " + tag); } bytes = new byte[length]; + // Note: do not tempt to call buffer.read(bytes) at all. There's a + // known bug that it returns -1 instead of 0. + if (length == 0) { + return bytes; + } if (buffer.read(bytes) != length) throw new IOException("short read on DerValue buffer"); if (isConstructed()) { diff -r d690c8a2acea -r d0bafa066816 jdk/test/sun/security/util/DerValue/EmptyValue.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/sun/security/util/DerValue/EmptyValue.java Mon Feb 23 10:04:52 2009 +0800 @@ -0,0 +1,44 @@ +/* + * Copyright 2009 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 6804045 + * @summary DerValue does not accept empty OCTET STRING + */ + +import sun.security.util.DerValue; + +public class EmptyValue { + + public static void main(String[] args) throws Exception { + DerValue v = new DerValue(new byte[]{4,0}); + if (v.getOctetString().length != 0) { + throw new Exception("Get octet string error"); + } + v = new DerValue(new byte[]{0x30,0}); + if (v.data.available() != 0) { + throw new Exception("Get sequence error"); + } + } +}