Skip to content

Commit c1a1645

Browse files
committedMay 3, 2024
8331655: ClassFile API ClassCastException with verbose output of certain class files
Reviewed-by: psandoz
1 parent 36c9607 commit c1a1645

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed
 

‎src/java.base/share/classes/jdk/internal/classfile/impl/ClassReaderImpl.java

+3-14
Original file line numberDiff line numberDiff line change
@@ -391,21 +391,10 @@ private PoolEntry entryByIndex(int index, int lowerBoundTag, int upperBoundTag)
391391

392392
@Override
393393
public AbstractPoolEntry.Utf8EntryImpl utf8EntryByIndex(int index) {
394-
if (index <= 0 || index >= constantPoolCount) {
395-
throw new ConstantPoolException("Bad CP UTF8 index: " + index);
396-
}
397-
PoolEntry info = cp[index];
398-
if (info == null) {
399-
int offset = cpOffset[index];
400-
int tag = readU1(offset);
401-
final int q = offset + 1;
402-
if (tag != TAG_UTF8) throw new ConstantPoolException("Not a UTF8 - index: " + index);
403-
AbstractPoolEntry.Utf8EntryImpl uinfo
404-
= new AbstractPoolEntry.Utf8EntryImpl(this, index, buffer, q + 2, readU2(q));
405-
cp[index] = uinfo;
406-
return uinfo;
394+
if (entryByIndex(index, TAG_UTF8, TAG_UTF8) instanceof AbstractPoolEntry.Utf8EntryImpl utf8) {
395+
return utf8;
407396
}
408-
return (AbstractPoolEntry.Utf8EntryImpl) info;
397+
throw new ConstantPoolException("Not a UTF8 - index: " + index);
409398
}
410399

411400
@Override

‎test/jdk/jdk/classfile/LimitsTest.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
/*
2525
* @test
26-
* @bug 8320360 8330684 8331320
26+
* @bug 8320360 8330684 8331320 8331655
2727
* @summary Testing ClassFile limits.
2828
* @run junit LimitsTest
2929
*/
@@ -36,6 +36,7 @@
3636
import java.lang.classfile.Opcode;
3737
import java.lang.classfile.attribute.CodeAttribute;
3838
import java.lang.classfile.constantpool.ConstantPoolException;
39+
import java.lang.classfile.constantpool.IntegerEntry;
3940
import jdk.internal.classfile.impl.DirectMethodBuilder;
4041
import jdk.internal.classfile.impl.LabelContext;
4142
import jdk.internal.classfile.impl.UnboundAttribute;
@@ -106,6 +107,14 @@ void testInvalidClassEntry() {
106107
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());
107108
}
108109

110+
@Test
111+
void testInvalidUtf8Entry() {
112+
var cp = ClassFile.of().parse(new byte[]{(byte)0xCA, (byte)0xFE, (byte)0xBA, (byte)0xBE,
113+
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();
114+
assertTrue(cp.entryByIndex(1) instanceof IntegerEntry); //parse valid int entry first
115+
assertThrows(ConstantPoolException.class, () -> cp.entryByIndex(2));
116+
}
117+
109118
@Test
110119
void testInvalidLookupSwitch() {
111120
assertThrows(IllegalArgumentException.class, () ->

1 commit comments

Comments
 (1)

openjdk-notifier[bot] commented on May 3, 2024

@openjdk-notifier[bot]
Please sign in to comment.