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 4c3f1dd00 StringUtils.repeat(String, [String,] int) now throws 
IllegalArgumentException (#1644)
4c3f1dd00 is described below

commit 4c3f1dd00ee7b8800478edb2b50dd687103b5f2a
Author: Gary Gregory <[email protected]>
AuthorDate: Fri May 15 07:33:39 2026 -0400

    StringUtils.repeat(String, [String,] int) now throws 
IllegalArgumentException (#1644)
    
    StringUtils.repeat(String, [String,] int) now throws 
IllegalArgumentException instead of NegativeArraySizeException (#1644).
---
 src/main/java/org/apache/commons/lang3/StringUtils.java     | 9 +++++++--
 src/test/java/org/apache/commons/lang3/StringUtilsTest.java | 9 ++++-----
 2 files changed, 11 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java 
b/src/main/java/org/apache/commons/lang3/StringUtils.java
index f5a9e6554..1b92bbab9 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -351,7 +351,7 @@ public static String abbreviate(final String str, final 
String abbrevMarker, fin
      * @throws IllegalArgumentException if the width is too small.
      * @since 3.6
      */
-    public static String abbreviate(final String str, String abbrevMarker, int 
offset, final int maxWidth) {
+    public static String abbreviate(final String str, String abbrevMarker, 
final int offset, final int maxWidth) {
         if (isEmpty(str)) {
             return str;
         }
@@ -6117,7 +6117,12 @@ public static String repeat(final String repeat, final 
int count) {
         if (inputLength == 1 && count <= PAD_LIMIT) {
             return repeat(repeat.charAt(0), count);
         }
-        final int outputLength = inputLength * count;
+        final int outputLength;
+        try {
+            outputLength = Math.multiplyExact(inputLength, count);
+        } catch (final Exception e) {
+            throw new IllegalArgumentException("The requested result is too 
large for a String.");
+        }
         switch (inputLength) {
         case 1:
             return repeat(repeat.charAt(0), count);
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index 2e6edbd18..df33386b2 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -1718,6 +1718,7 @@ void testRepeat_StringInt() {
         final String str = StringUtils.repeat("a", 10000);  // bigger than pad 
limit
         assertEquals(10000, str.length());
         assertTrue(StringUtils.containsOnly(str, 'a'));
+        assertThrows(IllegalArgumentException.class, () -> 
StringUtils.repeat("aa", 1_073_741_824));
     }
 
     @Test
@@ -1725,13 +1726,11 @@ void testRepeat_StringStringInt() {
         assertNull(StringUtils.repeat(null, null, 2));
         assertNull(StringUtils.repeat(null, "x", 2));
         assertEquals("", StringUtils.repeat("", null, 2));
-
         assertEquals("", StringUtils.repeat("ab", "", 0));
         assertEquals("", StringUtils.repeat("", "", 2));
-
         assertEquals("xx", StringUtils.repeat("", "x", 3));
-
         assertEquals("?, ?, ?", StringUtils.repeat("?", ", ", 3));
+        assertThrows(IllegalArgumentException.class, () -> 
StringUtils.repeat("?", ", ", 1_073_741_824));
     }
 
     /**
@@ -1739,7 +1738,7 @@ void testRepeat_StringStringInt() {
      */
     @Test
     void testReplace_StringStringArrayStringArray() {
-        //JAVADOC TESTS START
+        // JAVADOC TESTS START
         assertNull(StringUtils.replaceEach(null, new String[]{"a"}, new 
String[]{"b"}));
         assertEquals(StringUtils.replaceEach("", new String[]{"a"}, new 
String[]{"b"}), "");
         assertEquals(StringUtils.replaceEach("aba", null, null), "aba");
@@ -1751,7 +1750,7 @@ void testReplace_StringStringArrayStringArray() {
         assertEquals(StringUtils.replaceEach("aba", new String[]{null}, new 
String[]{"a"}), "aba");
         assertEquals(StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, 
new String[]{"w", "t"}), "wcte");
         assertEquals(StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, 
new String[]{"d", "t"}), "dcte");
-        //JAVADOC TESTS END
+        // JAVADOC TESTS END
 
         assertEquals("bcc", StringUtils.replaceEach("abc", new String[]{"a", 
"b"}, new String[]{"b", "c"}));
         assertEquals("q651.506bera", StringUtils.replaceEach("d216.102oren",

Reply via email to