JDK-8342702 : java.lang.classfile.MethodModel.code() throws unspecified java.lang.IllegalArgumentException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang.classfile
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • Submitted: 2024-10-21
  • Updated: 2024-10-21
  • Resolved: 2024-10-21
Related Reports
Relates :  
Description
When using the ClassFile, for certain contents of a class file, a call to MethodModel.code() throws an unspecified java.lang.IllegalArgumentException.

The sample code looks as follows:

    ClassFile cf = ClassFile.of(...);
    ClassModel model = cf.parse(...);
    for (final MethodModel method : model.methods()) {
        final Optional<CodeModel> methodCode = method.code();
        ...

This results in:


java.lang.IllegalArgumentException: Reading beyond classfile bounds
    at java.base/jdk.internal.classfile.impl.ClassReaderImpl.outOfBoundsError(ClassReaderImpl.java:179)
    at java.base/jdk.internal.classfile.impl.ClassReaderImpl.readU2(ClassReaderImpl.java:198)
    at java.base/jdk.internal.classfile.impl.BoundAttribute$BoundCodeAttribute.<init>(BoundAttribute.java:956)
    at java.base/jdk.internal.classfile.impl.CodeImpl.<init>(CodeImpl.java:83)
    at java.base/jdk.internal.classfile.impl.AbstractAttributeMapper$CodeMapper.readAttribute(AbstractAttributeMapper.java:164)
    at java.base/jdk.internal.classfile.impl.AbstractAttributeMapper$CodeMapper.readAttribute(AbstractAttributeMapper.java:155)
    at java.base/jdk.internal.classfile.impl.BoundAttribute.readAttributes(BoundAttribute.java:143)
    at java.base/jdk.internal.classfile.impl.MethodImpl.attributes(MethodImpl.java:94)
    at java.base/java.lang.classfile.AttributedElement.findAttribute(AttributedElement.java:64)
    at java.base/jdk.internal.classfile.impl.MethodImpl.code(MethodImpl.java:116)
    ...

This happens in Java 23 as well as latest JDK mainline (Java 24).

Comments
Unfortunately, as confusing as this arrangement is, throwing IAE upfront means we have to give up laziness. I thought the exception is specified in `java.lang.classfile` package; I am currently cleaning up specifications and can add this piece of info to the package-info.
21-10-2024

Hello Chen, given that the ClassFile.parse() invocation completes successfully but a subsequent call to MethodModel.code() throws the IllegalArgumentException, shouldn't that exception be specified on MethodModel.code() too?
21-10-2024

This is not a bug. `ClassFile::parse` specifes any malformed `class` file format is represented as an `IllegalArgumentException`.
21-10-2024