8357955: java.lang.classfile.Signature.ArrayTypeSig.of IAE not thrown for dims > 255

Reviewed-by: jlahoda
This commit is contained in:
Adam Sotona 2025-05-29 07:03:26 +00:00
parent 04e0fe00ab
commit d43f588db1
2 changed files with 22 additions and 5 deletions

View File

@ -417,15 +417,18 @@ public sealed interface Signature {
* {@return a signature for an array type}
* @param dims the dimension of the array
* @param componentSignature the component type
* @throws IllegalArgumentException if the resulting array type exceeds
* 255 dimensions
* @throws IllegalArgumentException if {@code dims < 1} or the
* resulting array type exceeds 255 dimensions
*/
public static ArrayTypeSig of(int dims, Signature componentSignature) {
requireNonNull(componentSignature);
if (componentSignature instanceof SignaturesImpl.ArrayTypeSigImpl arr) {
if (dims < 1 || dims > 255 - arr.arrayDepth())
throw new IllegalArgumentException("illegal array depth value");
return new SignaturesImpl.ArrayTypeSigImpl(dims + arr.arrayDepth(), arr.elemType());
}
if (dims < 1 || dims > 255)
throw new IllegalArgumentException("illegal array depth value");
if (componentSignature instanceof SignaturesImpl.ArrayTypeSigImpl arr)
return new SignaturesImpl.ArrayTypeSigImpl(dims + arr.arrayDepth(), arr.elemType());
return new SignaturesImpl.ArrayTypeSigImpl(dims, componentSignature);
}
}

View File

@ -24,7 +24,7 @@
/*
* @test
* @summary Testing Signatures.
* @bug 8321540 8319463
* @bug 8321540 8319463 8357955
* @run junit SignaturesTest
*/
import java.io.IOException;
@ -300,6 +300,20 @@ class SignaturesTest {
""".lines().forEach(assertThrows(MethodSignature::parseFrom));
}
@Test
void testArraySignatureLimits() {
var sig = Signature.parseFrom("I");
var arrSig = Signature.parseFrom("[I");
for (int dim : List.of(256, -1, 0))
Assertions.assertThrows(IllegalArgumentException.class, () -> Signature.ArrayTypeSig.of(dim, sig));
for (int dim : List.of(255, -1, 0))
Assertions.assertThrows(IllegalArgumentException.class, () -> Signature.ArrayTypeSig.of(dim, arrSig));
for (int dim : List.of(255, 1))
Signature.ArrayTypeSig.of(dim, sig);
for (int dim : List.of(254, 1))
Signature.ArrayTypeSig.of(dim, arrSig);
}
private Consumer<String> assertThrows(Function<String, ?> parser) {
return s -> Assertions.assertThrows(IllegalArgumentException.class, () -> parser.apply(s), s);
}