Skip to content

Commit 4c3f1dd

Browse files
authored
StringUtils.repeat(String, [String,] int) now throws IllegalArgumentException (#1644)
StringUtils.repeat(String, [String,] int) now throws IllegalArgumentException instead of NegativeArraySizeException (#1644).
1 parent 96e1e3d commit 4c3f1dd

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

src/main/java/org/apache/commons/lang3/StringUtils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public static String abbreviate(final String str, final String abbrevMarker, fin
351351
* @throws IllegalArgumentException if the width is too small.
352352
* @since 3.6
353353
*/
354-
public static String abbreviate(final String str, String abbrevMarker, int offset, final int maxWidth) {
354+
public static String abbreviate(final String str, String abbrevMarker, final int offset, final int maxWidth) {
355355
if (isEmpty(str)) {
356356
return str;
357357
}
@@ -6117,7 +6117,12 @@ public static String repeat(final String repeat, final int count) {
61176117
if (inputLength == 1 && count <= PAD_LIMIT) {
61186118
return repeat(repeat.charAt(0), count);
61196119
}
6120-
final int outputLength = inputLength * count;
6120+
final int outputLength;
6121+
try {
6122+
outputLength = Math.multiplyExact(inputLength, count);
6123+
} catch (final Exception e) {
6124+
throw new IllegalArgumentException("The requested result is too large for a String.");
6125+
}
61216126
switch (inputLength) {
61226127
case 1:
61236128
return repeat(repeat.charAt(0), count);

src/test/java/org/apache/commons/lang3/StringUtilsTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,28 +1718,27 @@ void testRepeat_StringInt() {
17181718
final String str = StringUtils.repeat("a", 10000); // bigger than pad limit
17191719
assertEquals(10000, str.length());
17201720
assertTrue(StringUtils.containsOnly(str, 'a'));
1721+
assertThrows(IllegalArgumentException.class, () -> StringUtils.repeat("aa", 1_073_741_824));
17211722
}
17221723

17231724
@Test
17241725
void testRepeat_StringStringInt() {
17251726
assertNull(StringUtils.repeat(null, null, 2));
17261727
assertNull(StringUtils.repeat(null, "x", 2));
17271728
assertEquals("", StringUtils.repeat("", null, 2));
1728-
17291729
assertEquals("", StringUtils.repeat("ab", "", 0));
17301730
assertEquals("", StringUtils.repeat("", "", 2));
1731-
17321731
assertEquals("xx", StringUtils.repeat("", "x", 3));
1733-
17341732
assertEquals("?, ?, ?", StringUtils.repeat("?", ", ", 3));
1733+
assertThrows(IllegalArgumentException.class, () -> StringUtils.repeat("?", ", ", 1_073_741_824));
17351734
}
17361735

17371736
/**
17381737
* Test method for 'StringUtils.replaceEach(String, String[], String[])'
17391738
*/
17401739
@Test
17411740
void testReplace_StringStringArrayStringArray() {
1742-
//JAVADOC TESTS START
1741+
// JAVADOC TESTS START
17431742
assertNull(StringUtils.replaceEach(null, new String[]{"a"}, new String[]{"b"}));
17441743
assertEquals(StringUtils.replaceEach("", new String[]{"a"}, new String[]{"b"}), "");
17451744
assertEquals(StringUtils.replaceEach("aba", null, null), "aba");
@@ -1751,7 +1750,7 @@ void testReplace_StringStringArrayStringArray() {
17511750
assertEquals(StringUtils.replaceEach("aba", new String[]{null}, new String[]{"a"}), "aba");
17521751
assertEquals(StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}), "wcte");
17531752
assertEquals(StringUtils.replaceEach("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}), "dcte");
1754-
//JAVADOC TESTS END
1753+
// JAVADOC TESTS END
17551754

17561755
assertEquals("bcc", StringUtils.replaceEach("abc", new String[]{"a", "b"}, new String[]{"b", "c"}));
17571756
assertEquals("q651.506bera", StringUtils.replaceEach("d216.102oren",

0 commit comments

Comments
 (0)