diff --git a/src/java.base/share/classes/jdk/internal/classfile/impl/ClassReaderImpl.java b/src/java.base/share/classes/jdk/internal/classfile/impl/ClassReaderImpl.java index 2022f8f0154..4878ad56ae8 100644 --- a/src/java.base/share/classes/jdk/internal/classfile/impl/ClassReaderImpl.java +++ b/src/java.base/share/classes/jdk/internal/classfile/impl/ClassReaderImpl.java @@ -391,21 +391,10 @@ public final class ClassReaderImpl @Override public AbstractPoolEntry.Utf8EntryImpl utf8EntryByIndex(int index) { - if (index <= 0 || index >= constantPoolCount) { - throw new ConstantPoolException("Bad CP UTF8 index: " + index); + if (entryByIndex(index, TAG_UTF8, TAG_UTF8) instanceof AbstractPoolEntry.Utf8EntryImpl utf8) { + return utf8; } - PoolEntry info = cp[index]; - if (info == null) { - int offset = cpOffset[index]; - int tag = readU1(offset); - final int q = offset + 1; - if (tag != TAG_UTF8) throw new ConstantPoolException("Not a UTF8 - index: " + index); - AbstractPoolEntry.Utf8EntryImpl uinfo - = new AbstractPoolEntry.Utf8EntryImpl(this, index, buffer, q + 2, readU2(q)); - cp[index] = uinfo; - return uinfo; - } - return (AbstractPoolEntry.Utf8EntryImpl) info; + throw new ConstantPoolException("Not a UTF8 - index: " + index); } @Override diff --git a/test/jdk/jdk/classfile/LimitsTest.java b/test/jdk/jdk/classfile/LimitsTest.java index 5b39c0d4985..8a8d8bddc35 100644 --- a/test/jdk/jdk/classfile/LimitsTest.java +++ b/test/jdk/jdk/classfile/LimitsTest.java @@ -23,7 +23,7 @@ /* * @test - * @bug 8320360 8330684 8331320 + * @bug 8320360 8330684 8331320 8331655 * @summary Testing ClassFile limits. * @run junit LimitsTest */ @@ -36,6 +36,7 @@ import java.lang.classfile.ClassFile; import java.lang.classfile.Opcode; import java.lang.classfile.attribute.CodeAttribute; import java.lang.classfile.constantpool.ConstantPoolException; +import java.lang.classfile.constantpool.IntegerEntry; import jdk.internal.classfile.impl.DirectMethodBuilder; import jdk.internal.classfile.impl.LabelContext; import jdk.internal.classfile.impl.UnboundAttribute; @@ -106,6 +107,14 @@ class LimitsTest { 0, 0, 0, 0, 0, 2, ClassFile.TAG_METHODREF, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}).thisClass()); } + @Test + void testInvalidUtf8Entry() { + var cp = ClassFile.of().parse(new byte[]{(byte)0xCA, (byte)0xFE, (byte)0xBA, (byte)0xBE, + 0, 0, 0, 0, 0, 3, ClassFile.TAG_INTEGER, 0, 0, 0, 0, ClassFile.TAG_NAMEANDTYPE, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}).constantPool(); + assertTrue(cp.entryByIndex(1) instanceof IntegerEntry); //parse valid int entry first + assertThrows(ConstantPoolException.class, () -> cp.entryByIndex(2)); + } + @Test void testInvalidLookupSwitch() { assertThrows(IllegalArgumentException.class, () ->