8283075: Bad IllegalArgumentException message for out of range rank from ClassDesc.arrayType(int)

Reviewed-by: vromero, alanb
This commit is contained in:
Joe Darcy 2022-03-15 17:33:45 +00:00
parent 12dca36c73
commit 32f8437d85
2 changed files with 35 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018, 2022, 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
@ -148,7 +148,8 @@ public sealed interface ClassDesc
* is described by this {@linkplain ClassDesc}.
*
* @return a {@linkplain ClassDesc} describing the array type
* @throws IllegalStateException if the resulting {@linkplain ClassDesc} would have an array rank of greater than 255
* @throws IllegalStateException if the resulting {@linkplain
* ClassDesc} would have an array rank of greater than 255
* @jvms 4.4.1 The CONSTANT_Class_info Structure
*/
default ClassDesc arrayType() {
@ -167,14 +168,27 @@ public sealed interface ClassDesc
*
* @param rank the rank of the array
* @return a {@linkplain ClassDesc} describing the array type
* @throws IllegalArgumentException if the rank is less than or equal to zero or if the rank of the resulting array type is
* @throws IllegalArgumentException if the rank is less than or
* equal to zero or if the rank of the resulting array type is
* greater than 255
* @jvms 4.4.1 The CONSTANT_Class_info Structure
*/
default ClassDesc arrayType(int rank) {
int currentDepth = ConstantUtils.arrayDepth(descriptorString());
if (rank <= 0 || currentDepth + rank > ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS)
throw new IllegalArgumentException("rank: " + currentDepth + rank);
int netRank;
if (rank <= 0) {
throw new IllegalArgumentException("rank " + rank + " is not a positive value");
}
try {
int currentDepth = ConstantUtils.arrayDepth(descriptorString());
netRank = Math.addExact(currentDepth, rank);
if (netRank > ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS) {
throw new IllegalArgumentException("rank: " + netRank +
" exceeds maximum supported dimension of " +
ConstantUtils.MAX_ARRAY_TYPE_DESC_DIMENSIONS);
}
} catch (ArithmeticException ae) {
throw new IllegalArgumentException("Integer overflow in rank computation");
}
return ClassDesc.ofDescriptor("[".repeat(rank) + descriptorString());
}

View File

@ -42,7 +42,7 @@ import static org.testng.Assert.fail;
/**
* @test
* @bug 8215510
* @bug 8215510 8283075
* @compile ClassDescTest.java
* @run testng ClassDescTest
* @summary unit tests for java.lang.constant.ClassDesc
@ -184,6 +184,19 @@ public class ClassDescTest extends SymbolicDescTest {
}
}
private void testArrayRankOverflow() {
ClassDesc TwoDArrayDesc =
String.class.describeConstable().get().arrayType().arrayType();
try {
TwoDArrayDesc.arrayType(Integer.MAX_VALUE);
fail("");
} catch (IllegalArgumentException iae) {
// Expected
}
}
public void testArrayClassDesc() throws ReflectiveOperationException {
for (String d : basicDescs) {
ClassDesc a0 = ClassDesc.ofDescriptor(d);
@ -218,6 +231,7 @@ public class ClassDescTest extends SymbolicDescTest {
testBadArrayRank(ConstantDescs.CD_int);
testBadArrayRank(ConstantDescs.CD_String);
testBadArrayRank(ClassDesc.of("Bar"));
testArrayRankOverflow();
}
}