Skip to content

Commit d43f588

Browse files
committed
8357955: java.lang.classfile.Signature.ArrayTypeSig.of IAE not thrown for dims > 255
Reviewed-by: jlahoda
1 parent 04e0fe0 commit d43f588

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/java.base/share/classes/java/lang/classfile/Signature.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -417,15 +417,18 @@ public static ArrayTypeSig of(Signature componentSignature) {
417417
* {@return a signature for an array type}
418418
* @param dims the dimension of the array
419419
* @param componentSignature the component type
420-
* @throws IllegalArgumentException if the resulting array type exceeds
421-
* 255 dimensions
420+
* @throws IllegalArgumentException if {@code dims < 1} or the
421+
* resulting array type exceeds 255 dimensions
422422
*/
423423
public static ArrayTypeSig of(int dims, Signature componentSignature) {
424424
requireNonNull(componentSignature);
425+
if (componentSignature instanceof SignaturesImpl.ArrayTypeSigImpl arr) {
426+
if (dims < 1 || dims > 255 - arr.arrayDepth())
427+
throw new IllegalArgumentException("illegal array depth value");
428+
return new SignaturesImpl.ArrayTypeSigImpl(dims + arr.arrayDepth(), arr.elemType());
429+
}
425430
if (dims < 1 || dims > 255)
426431
throw new IllegalArgumentException("illegal array depth value");
427-
if (componentSignature instanceof SignaturesImpl.ArrayTypeSigImpl arr)
428-
return new SignaturesImpl.ArrayTypeSigImpl(dims + arr.arrayDepth(), arr.elemType());
429432
return new SignaturesImpl.ArrayTypeSigImpl(dims, componentSignature);
430433
}
431434
}

test/jdk/jdk/classfile/SignaturesTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
/*
2525
* @test
2626
* @summary Testing Signatures.
27-
* @bug 8321540 8319463
27+
* @bug 8321540 8319463 8357955
2828
* @run junit SignaturesTest
2929
*/
3030
import java.io.IOException;
@@ -300,6 +300,20 @@ void testBadMethodSignatures() {
300300
""".lines().forEach(assertThrows(MethodSignature::parseFrom));
301301
}
302302

303+
@Test
304+
void testArraySignatureLimits() {
305+
var sig = Signature.parseFrom("I");
306+
var arrSig = Signature.parseFrom("[I");
307+
for (int dim : List.of(256, -1, 0))
308+
Assertions.assertThrows(IllegalArgumentException.class, () -> Signature.ArrayTypeSig.of(dim, sig));
309+
for (int dim : List.of(255, -1, 0))
310+
Assertions.assertThrows(IllegalArgumentException.class, () -> Signature.ArrayTypeSig.of(dim, arrSig));
311+
for (int dim : List.of(255, 1))
312+
Signature.ArrayTypeSig.of(dim, sig);
313+
for (int dim : List.of(254, 1))
314+
Signature.ArrayTypeSig.of(dim, arrSig);
315+
}
316+
303317
private Consumer<String> assertThrows(Function<String, ?> parser) {
304318
return s -> Assertions.assertThrows(IllegalArgumentException.class, () -> parser.apply(s), s);
305319
}

0 commit comments

Comments
 (0)