8366028: MethodType::fromMethodDescriptorString should not throw UnsupportedOperationException for invalid descriptors

Reviewed-by: jvernee
This commit is contained in:
Chen Liang 2025-08-27 14:25:39 +00:00
parent 124575b4c2
commit 1d53ac30f1
2 changed files with 32 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2025, 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
@ -96,8 +96,15 @@ public class BytecodeDescriptor {
}
} else if (c == '[') {
Class<?> t = parseSig(str, i, end, loader);
if (t != null)
t = t.arrayType();
if (t != null) {
try {
t = t.arrayType();
} catch (UnsupportedOperationException ex) {
// Bad arrays, such as [V or more than 255 dims
// We have a more informative IAE
return null;
}
}
return t;
} else {
return Wrapper.forBasicType(c).primitiveType();

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2025, 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
@ -22,6 +22,7 @@
*/
/* @test
* @bug 8366028
* @summary unit tests for java.lang.invoke.MethodType
* @compile MethodTypeTest.java
* @run testng/othervm test.java.lang.invoke.MethodTypeTest
@ -35,6 +36,8 @@ import java.lang.reflect.Method;
import java.util.*;
import org.testng.*;
import static org.testng.Assert.assertThrows;
import static org.testng.AssertJUnit.*;
import org.testng.annotations.*;
@ -218,6 +221,24 @@ public class MethodTypeTest {
return sb.toString().replace('.', '/');
}
@DataProvider(name = "badMethodDescriptorStrings")
public String[] badMethodDescriptorStrings() {
return new String[] {
"(I)",
"(V)V",
"([V)V",
"(" + "[".repeat(256) + "J)I",
"(java/lang/Object)V",
"()java/lang/Object",
};
}
// JDK-8366028
@Test(dataProvider = "badMethodDescriptorStrings", expectedExceptions = IllegalArgumentException.class)
public void testFromMethodDescriptorStringNegatives(String desc) {
MethodType.fromMethodDescriptorString(desc, null);
}
@Test
public void testHasPrimitives() {
System.out.println("hasPrimitives");