Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public void setLabelTarget(Label label, int bci) {

@Override
public Label getLabel(int bci) {
if (bci < 0 || bci > codeLength)
throw new IllegalArgumentException(String.format("Bytecode offset out of range; bci=%d, codeLength=%d",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we get an IAE formatter in jdk.internal.util.Preconditons for classfile-specific bound checking, so we can do Preconditions.checkIndex(bci, codeLength, Preconditions.IAE_FORMATTER); and share it with other use sites of classfile-specific exceptions.

In addition, we might move the new IllegalArgumentException constructor calls to a utility method in our Util class in case we migrate our exceptions to a new subclass of IAE. Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is a bit overkill for this simple fix. We may discuss it as a part of some wider code cleanups.

bci, codeLength));
if (labels == null)
labels = new LabelImpl[codeLength + 1];
LabelImpl l = labels[bci];
Expand Down
10 changes: 10 additions & 0 deletions test/jdk/jdk/classfile/LimitsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.lang.constant.ConstantDescs;
import java.lang.constant.MethodTypeDesc;
import jdk.internal.classfile.Classfile;
import jdk.internal.classfile.impl.LabelContext;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

Expand Down Expand Up @@ -71,4 +72,13 @@ void testEmptyCode() {
assertThrows(IllegalArgumentException.class, () -> Classfile.of().build(ClassDesc.of("EmptyClass"), cb -> cb.withMethodBody(
"emptyMethod", MethodTypeDesc.of(ConstantDescs.CD_void), 0, cob -> {})));
}

@Test
void testCodeRange() {
var cf = Classfile.of();
var lc = (LabelContext)cf.parse(cf.build(ClassDesc.of("EmptyClass"), cb -> cb.withMethodBody(
"aMethod", MethodTypeDesc.of(ConstantDescs.CD_void), 0, cob -> cob.return_()))).methods().get(0).code().get();
assertThrows(IllegalArgumentException.class, () -> lc.getLabel(-1));
assertThrows(IllegalArgumentException.class, () -> lc.getLabel(10));
}
}