From a2d3fc83b0dd7eea38e1dd5898a97d6d7ff60194 Mon Sep 17 00:00:00 2001 From: SirYwell Date: Sat, 29 Apr 2023 07:48:14 +0000 Subject: [PATCH] 8304837: Classfile API throws IOOBE for MethodParameters attribute without parameter names Reviewed-by: asotona, jwaters, vromero --- .../classfile/impl/BoundAttribute.java | 4 +- .../jdk/jdk/classfile/BoundAttributeTest.java | 71 +++++++++++++++++++ 2 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 test/jdk/jdk/classfile/BoundAttributeTest.java diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/BoundAttribute.java b/src/java.base/share/classes/jdk/internal/classfile/impl/BoundAttribute.java index d7243e27345..c7fd1908ca8 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/BoundAttribute.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/BoundAttribute.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2023, 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 @@ -360,7 +360,7 @@ public abstract sealed class BoundAttribute> int p = payloadStart + 1; int pEnd = p + (cnt * 4); for (int i = 0; p < pEnd; p += 4, i++) { - Utf8Entry name = classReader.readUtf8Entry(p); + Utf8Entry name = classReader.readUtf8EntryOrNull(p); int accessFlags = classReader.readU2(p + 2); elements[i] = MethodParameterInfo.of(Optional.ofNullable(name), accessFlags); } diff --git a/test/jdk/jdk/classfile/BoundAttributeTest.java b/test/jdk/jdk/classfile/BoundAttributeTest.java new file mode 100644 index 00000000000..3dc09a79bc0 --- /dev/null +++ b/test/jdk/jdk/classfile/BoundAttributeTest.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2023, 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. + */ + +/* + * @test + * @bug 8304837 + * @summary Testing BoundAttributes + * @run junit BoundAttributeTest + */ +import jdk.internal.classfile.Attributes; +import jdk.internal.classfile.ClassModel; +import jdk.internal.classfile.Classfile; +import jdk.internal.classfile.CodeBuilder; +import jdk.internal.classfile.attribute.MethodParameterInfo; +import jdk.internal.classfile.attribute.MethodParametersAttribute; +import org.junit.jupiter.api.Test; +import org.opentest4j.AssertionFailedError; + +import java.lang.constant.ClassDesc; +import java.lang.constant.ConstantDescs; +import java.lang.constant.MethodTypeDesc; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class BoundAttributeTest { + + @Test + void testReadMethodParametersAttributeWithoutParameterName() { + // build a simple method: void method(int) + MethodTypeDesc methodTypeDesc = MethodTypeDesc.of(ConstantDescs.CD_void, ConstantDescs.CD_int); + byte[] raw = Classfile.build(ClassDesc.of("TestClass"), builder -> { + builder.withMethod("method", methodTypeDesc, 0, mb -> { + mb.withCode(CodeBuilder::return_); + // add a MethodParameters attribute without name for the parameter + mb.with(MethodParametersAttribute.of(MethodParameterInfo.ofParameter(Optional.empty(), 0))); + }); + }); + ClassModel model = Classfile.parse(raw); + MethodParametersAttribute methodParametersAttribute = model.methods().get(0) + .findAttribute(Attributes.METHOD_PARAMETERS) + .orElseThrow(() -> new AssertionFailedError("Attribute not present")); + // MethodParametersAttribute#parameters() materializes the parameters + List parameters = assertDoesNotThrow(methodParametersAttribute::parameters); + assertTrue(parameters.get(0).name().isEmpty()); + } +}