Skip to content

Commit 42ccb74

Browse files
committedMay 15, 2024
8331940: ClassFile API ArrayIndexOutOfBoundsException with certain class files
Reviewed-by: liach, psandoz
1 parent 61aff6d commit 42ccb74

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed
 

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -237,6 +237,10 @@ private void inflateLineNumbers() {
237237
int pEnd = p + (nLn * 4);
238238
for (; p < pEnd; p += 4) {
239239
int startPc = classReader.readU2(p);
240+
if (startPc > codeLength) {
241+
throw new IllegalArgumentException(String.format(
242+
"Line number start_pc out of range; start_pc=%d, codeLength=%d", startPc, codeLength));
243+
}
240244
int lineNumber = classReader.readU2(p + 2);
241245
lineNumbers[startPc] = lineNumber;
242246
}

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

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

2424
/*
2525
* @test
26-
* @bug 8320360 8330684 8331320 8331655
26+
* @bug 8320360 8330684 8331320 8331655 8331940
2727
* @summary Testing ClassFile limits.
2828
* @run junit LimitsTest
2929
*/
@@ -35,8 +35,12 @@
3535
import java.lang.classfile.ClassFile;
3636
import java.lang.classfile.Opcode;
3737
import java.lang.classfile.attribute.CodeAttribute;
38+
import java.lang.classfile.attribute.LineNumberInfo;
39+
import java.lang.classfile.attribute.LineNumberTableAttribute;
3840
import java.lang.classfile.constantpool.ConstantPoolException;
3941
import java.lang.classfile.constantpool.IntegerEntry;
42+
import java.util.List;
43+
import jdk.internal.classfile.impl.DirectCodeBuilder;
4044
import jdk.internal.classfile.impl.DirectMethodBuilder;
4145
import jdk.internal.classfile.impl.LabelContext;
4246
import jdk.internal.classfile.impl.UnboundAttribute;
@@ -161,4 +165,14 @@ public void writeBody(BufWriter b) {
161165
b.writeU2(0);//attributes
162166
}})))).methods().get(0).code().get().elementList());
163167
}
168+
169+
@Test
170+
void testLineNumberOutOfBounds() {
171+
assertThrows(IllegalArgumentException.class, () ->
172+
ClassFile.of().parse(ClassFile.of().build(ClassDesc.of("LineNumberClass"), cb -> cb.withMethodBody(
173+
"lineNumberMethod", MethodTypeDesc.of(ConstantDescs.CD_void), 0, cob -> ((DirectCodeBuilder)cob
174+
.return_())
175+
.writeAttribute(LineNumberTableAttribute.of(List.of(LineNumberInfo.of(500, 0))))
176+
))).methods().get(0).code().get().elementList());
177+
}
164178
}

0 commit comments

Comments
 (0)