This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git
The following commit(s) were added to refs/heads/master by this push:
new 8702a433f Reject incomplete array prefixes in ClassUtils
8702a433f is described below
commit 8702a433f0aff434b4c255eba0c27b54a33e6457
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Nov 18 14:14:19 2026 -0500
Reject incomplete array prefixes in ClassUtils
Throw IllegalArgumentException instead of
StringIndexOutOfBoundsException
when getPackageName(String) or getShortClassName(String) receives only
'[' characters. Preserve the existing array-prefix optimization and
handling of other names.
Clarify the limited validation in Javadoc, add a release note, and test
exception messages, short and long bracket-only inputs, and valid
arrays.
Assisted-By: OpenAI Codex
---
src/changes/changes.xml | 1 +
.../java/org/apache/commons/lang3/ClassUtils.java | 17 +++++++++-----
.../org/apache/commons/lang3/ClassUtilsTest.java | 26 ++++++++++++++++++----
3 files changed, 35 insertions(+), 9 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index e75779ddf..22e5b1c2e 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -107,6 +107,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="fix" dev="ggregory" due-to="Gary Gregory,
Amar Ghugare">Preserve defining class loaders throughout object graphs in
SerializationUtils.clone() and roundtrip().</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory,
Faris Basha TM">Javadoc: Fix dead links to the 3.6 release notes
(#1885).</action>
<action type="fix" dev="ggregory" due-to="Gary Gregory,
joao-jordy">Avoid repeated array prefix copies in
ClassUtils.getShortClassName(String) (#1886).</action>
+ <action type="fix" dev="ggregory" due-to="Gary Gregory,
joao-jordy">Throw IllegalArgumentException instead of
StringIndexOutOfBoundsException for bracket-only names in
ClassUtils.getPackageName(String) and getShortClassName(String)
(#1887).</action>
<!-- ADD -->
<!-- UPDATE -->
</release>
diff --git a/src/main/java/org/apache/commons/lang3/ClassUtils.java
b/src/main/java/org/apache/commons/lang3/ClassUtils.java
index 5c07a801d..66279b957 100644
--- a/src/main/java/org/apache/commons/lang3/ClassUtils.java
+++ b/src/main/java/org/apache/commons/lang3/ClassUtils.java
@@ -931,6 +931,7 @@ public static String getPackageName(final Object object,
final String valueIfNul
*
* @param className The className to get the package name for, may be
{@code null}.
* @return The package name or an empty string.
+ * @throws IllegalArgumentException if the name consists only of {@code
'['} characters.
*/
public static String getPackageName(String className) {
if (StringUtils.isEmpty(className)) {
@@ -938,9 +939,12 @@ public static String getPackageName(String className) {
}
int i = 0;
// Strip array encoding
- while (className.charAt(i) == '[') {
+ while (i < className.length() && className.charAt(i) == '[') {
i++;
}
+ if (i == className.length()) {
+ throw new IllegalArgumentException(String.format("Invalid class
name %s", className));
+ }
className = className.substring(i);
// Strip Object type encoding
if (className.charAt(0) == 'L' && className.charAt(className.length()
- 1) == ';') {
@@ -1191,9 +1195,8 @@ public static String getShortClassName(final Object
object, final String valueIf
* Gets the class name minus the package name from a String.
*
* <p>
- * The string passed in is assumed to be a class name - it is not checked.
The string has to be formatted the way as the
- * JDK method {@code Class.getName()} returns it, and not the usual way as
we write it, for example in import
- * statements, or as it is formatted by {@code Class.getCanonicalName()}.
+ * The string passed in is assumed to be a class name, but is not fully
validated. It must use the format returned by {@link Class#getName()},
+ * rather than the form used in import statements or returned by {@link
Class#getCanonicalName()}.
* </p>
*
* <p>
@@ -1217,6 +1220,7 @@ public static String getShortClassName(final Object
object, final String valueIf
* {@code Class.getName()} and not {@code Class.getCanonicalName()}.
* @return The class name of the class without the package name or an
empty string. If the class is an inner class then
* value contains the outer class or classes and the separator is
replaced to be {@code .} (dot) character.
+ * @throws IllegalArgumentException if the name consists only of {@code
'['} characters.
*/
public static String getShortClassName(String className) {
if (StringUtils.isEmpty(className)) {
@@ -1227,10 +1231,13 @@ public static String getShortClassName(String
className) {
if (className.startsWith("[")) {
// Strip the prefix once to avoid copying the string for each
dimension.
int dim = 0;
- while (className.charAt(dim) == '[') {
+ while (dim < className.length() && className.charAt(dim) == '[') {
dim++;
arrayPrefix.append("[]");
}
+ if (dim == className.length()) {
+ throw new IllegalArgumentException(String.format("Invalid
class name %s", className));
+ }
className = className.substring(dim);
// Strip Object type encoding
if (className.charAt(0) == 'L' &&
className.charAt(className.length() - 1) == ';') {
diff --git a/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
b/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
index e98f4b79a..58c9247a5 100644
--- a/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/ClassUtilsTest.java
@@ -547,6 +547,23 @@ void test_getPackageName_String() {
assertEquals("", ClassUtils.getPackageName(""));
}
+ @ParameterizedTest
+ @ValueSource(ints = { 1, 2, MAX_ARRAY_DIMENSIONS, 1000 })
+ void test_getPackageName_String_Array(final int dimensions) {
+ final String prefix = StringUtils.repeat('[', dimensions);
+ assertEquals("", ClassUtils.getPackageName(prefix + "I"));
+ assertEquals("java.lang", ClassUtils.getPackageName(prefix +
"Ljava.lang.String;"));
+ assertEquals("java.util", ClassUtils.getPackageName(prefix +
"Ljava.util.Map$Entry;"));
+ assertEquals("", ClassUtils.getPackageName(prefix + "LOuter$Inner;"));
+ }
+
+ @ParameterizedTest
+ @ValueSource(ints = { 1, 2, 3, MAX_ARRAY_DIMENSIONS, MAX_ARRAY_DIMENSIONS
+ 1, 1000 })
+ void test_getPackageName_String_BracketsOnly(final int dimensions) {
+ final String className = StringUtils.repeat('[', dimensions);
+ assertEquals("Invalid class name " + className,
assertIllegalArgumentException(() ->
ClassUtils.getPackageName(className)).getMessage());
+ }
+
@Test
void test_getShortCanonicalName_Class() {
assertEquals("ClassUtils",
ClassUtils.getShortCanonicalName(ClassUtils.class));
@@ -739,14 +756,15 @@ void test_getShortClassName_String_Array(final int
dimensions) {
}
@ParameterizedTest
- @ValueSource(strings = { "[", "[[", "[[[" })
- void test_getShortClassName_String_BracketsOnly(final String className) {
- assertThrows(StringIndexOutOfBoundsException.class, () ->
ClassUtils.getShortClassName(className));
+ @ValueSource(ints = { 1, 2, 3, MAX_ARRAY_DIMENSIONS, MAX_ARRAY_DIMENSIONS
+ 1, 1000 })
+ void test_getShortClassName_String_BracketsOnly(final int dimensions) {
+ final String className = StringUtils.repeat('[', dimensions);
+ assertEquals("Invalid class name " + className,
assertIllegalArgumentException(() ->
ClassUtils.getShortClassName(className)).getMessage());
}
@Test
void test_getShortClassName_String_ExcessiveDimensions() {
- // This method does not validate class names, including dimensions
beyond the JVM limit.
+ // This method does not validate dimensions beyond the JVM limit.
final int dimensions = 1000;
final String className = StringUtils.repeat('[', dimensions) +
"Ljava.lang.String;";
assertEquals("String" + StringUtils.repeat("[]", dimensions),
ClassUtils.getShortClassName(className));