From 1b9d938d66a172b200fd90d4a61707852bdb4b8f Mon Sep 17 00:00:00 2001 From: Loic Guibert Date: Thu, 23 Apr 2015 15:41:51 +0400 Subject: [PATCH 1/3] LANG-1118 Fix StringUtils.repeat(char, int) dealing with negative repeat value --- src/main/java/org/apache/commons/lang3/StringUtils.java | 3 +++ .../java/org/apache/commons/lang3/StringUtilsTest.java | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java b/src/main/java/org/apache/commons/lang3/StringUtils.java index 360185c17..26245726d 100644 --- a/src/main/java/org/apache/commons/lang3/StringUtils.java +++ b/src/main/java/org/apache/commons/lang3/StringUtils.java @@ -5233,6 +5233,9 @@ public class StringUtils { * @see #repeat(String, int) */ public static String repeat(final char ch, final int repeat) { + if (repeat <= 0) { + return EMPTY; + } final char[] buf = new char[repeat]; for (int i = repeat - 1; i >= 0; i--) { buf[i] = ch; diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index 5721dbc70..8cac69696 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -1374,6 +1374,13 @@ public class StringUtilsTest { assertEquals("?, ?, ?", StringUtils.repeat("?", ", ", 3)); } + @Test + public void testRepeat_CharInt() { + assertEquals("zzz", StringUtils.repeat('z', 3)); + assertEquals("", StringUtils.repeat('z', 0)); + assertEquals("", StringUtils.repeat('z', -2)); + } + @Test public void testChop() { From 4ad74f449258781e9df189413423ffd6ad95c526 Mon Sep 17 00:00:00 2001 From: Loic Guibert Date: Thu, 23 Apr 2015 15:43:21 +0400 Subject: [PATCH 2/3] LANG-1118 Adding a test case for StringUtils.repeat(String, int) checking dealing with negative repeat value --- src/test/java/org/apache/commons/lang3/StringUtilsTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java index 8cac69696..3addd78a2 100644 --- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java @@ -1353,6 +1353,7 @@ public class StringUtilsTest { assertEquals("", StringUtils.repeat("ab", 0)); assertEquals("", StringUtils.repeat("", 3)); assertEquals("aaa", StringUtils.repeat("a", 3)); + assertEquals("", StringUtils.repeat("a", -2)); assertEquals("ababab", StringUtils.repeat("ab", 3)); assertEquals("abcabcabc", StringUtils.repeat("abc", 3)); final String str = StringUtils.repeat("a", 10000); // bigger than pad limit From 84c5e8c7124dbecc9243dbd0313b3b94c7185f63 Mon Sep 17 00:00:00 2001 From: Benedikt Ritter Date: Tue, 28 Apr 2015 21:35:16 +0200 Subject: [PATCH 3/3] Add LANG-1118 to changes.xml --- src/changes/changes.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 5918d808d..d2416b4b9 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -22,6 +22,7 @@ + StringUtils.repeat('z', -1) throws NegativeArraySizeException Add swap and shift operations for arrays to ArrayUtils TypeUtils.parameterizeWithOwner - wrong format descriptor for "invalid number of type parameters". MultilineRecursiveToStringStyle largely unusable due to being package-private.