8331940: ClassFile API ArrayIndexOutOfBoundsException with certain class files

Reviewed-by: liach, psandoz
This commit is contained in:
Adam Sotona 2024-05-15 16:14:15 +00:00
parent 61aff6db15
commit 42ccb74399
2 changed files with 20 additions and 2 deletions
src/java.base/share/classes/jdk/internal/classfile/impl
test/jdk/jdk/classfile

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 2024, 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
@ -237,6 +237,10 @@ public final class CodeImpl
int pEnd = p + (nLn * 4);
for (; p < pEnd; p += 4) {
int startPc = classReader.readU2(p);
if (startPc > codeLength) {
throw new IllegalArgumentException(String.format(
"Line number start_pc out of range; start_pc=%d, codeLength=%d", startPc, codeLength));
}
int lineNumber = classReader.readU2(p + 2);
lineNumbers[startPc] = lineNumber;
}

@ -23,7 +23,7 @@
/*
* @test
* @bug 8320360 8330684 8331320 8331655
* @bug 8320360 8330684 8331320 8331655 8331940
* @summary Testing ClassFile limits.
* @run junit LimitsTest
*/
@ -35,8 +35,12 @@ import java.lang.constant.MethodTypeDesc;
import java.lang.classfile.ClassFile;
import java.lang.classfile.Opcode;
import java.lang.classfile.attribute.CodeAttribute;
import java.lang.classfile.attribute.LineNumberInfo;
import java.lang.classfile.attribute.LineNumberTableAttribute;
import java.lang.classfile.constantpool.ConstantPoolException;
import java.lang.classfile.constantpool.IntegerEntry;
import java.util.List;
import jdk.internal.classfile.impl.DirectCodeBuilder;
import jdk.internal.classfile.impl.DirectMethodBuilder;
import jdk.internal.classfile.impl.LabelContext;
import jdk.internal.classfile.impl.UnboundAttribute;
@ -161,4 +165,14 @@ class LimitsTest {
b.writeU2(0);//attributes
}})))).methods().get(0).code().get().elementList());
}
@Test
void testLineNumberOutOfBounds() {
assertThrows(IllegalArgumentException.class, () ->
ClassFile.of().parse(ClassFile.of().build(ClassDesc.of("LineNumberClass"), cb -> cb.withMethodBody(
"lineNumberMethod", MethodTypeDesc.of(ConstantDescs.CD_void), 0, cob -> ((DirectCodeBuilder)cob
.return_())
.writeAttribute(LineNumberTableAttribute.of(List.of(LineNumberInfo.of(500, 0))))
))).methods().get(0).code().get().elementList());
}
}