8331655: ClassFile API ClassCastException with verbose output of certain class files

Reviewed-by: psandoz
This commit is contained in:
Adam Sotona 2024-05-03 19:15:12 +00:00
parent 36c9607f66
commit c1a164528a
2 changed files with 13 additions and 15 deletions
src/java.base/share/classes/jdk/internal/classfile/impl
test/jdk/jdk/classfile

@ -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

@ -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, () ->