This is an automated email from the ASF dual-hosted git repository.

ggregory 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 d62d4e2  [LANG-1528] replaceEachRepeatedly gives IllegalStateException 
(#505)
d62d4e2 is described below

commit d62d4e21ef20ee01e65cbf5257e04d6b572aa73b
Author: Edwin Delgado H <1545116+edelga...@users.noreply.github.com>
AuthorDate: Sat Jun 13 12:31:07 2020 -0300

    [LANG-1528] replaceEachRepeatedly gives IllegalStateException (#505)
    
    * Fix https://issues.apache.org/jira/browse/LANG-1528
    
    * Fix https://issues.apache.org/jira/browse/LANG-1528
    
    Co-authored-by: Edwin DH <peo_ehuaynal...@uolinc.com>
---
 .../java/org/apache/commons/lang3/StringUtils.java  | 19 +++++++++++++------
 .../org/apache/commons/lang3/StringUtilsTest.java   | 21 +++++++++++----------
 2 files changed, 24 insertions(+), 16 deletions(-)

diff --git a/src/main/java/org/apache/commons/lang3/StringUtils.java 
b/src/main/java/org/apache/commons/lang3/StringUtils.java
index f9143c5..7dc9286 100644
--- a/src/main/java/org/apache/commons/lang3/StringUtils.java
+++ b/src/main/java/org/apache/commons/lang3/StringUtils.java
@@ -25,6 +25,8 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
 import java.util.Objects;
+import java.util.HashSet;
+import java.util.Set;
 import java.util.function.Supplier;
 import java.util.regex.Pattern;
 
@@ -6684,14 +6686,19 @@ public class StringUtils {
         // mchyzer Performance note: This creates very few new objects (one 
major goal)
         // let me know if there are performance requests, we can create a 
harness to measure
 
-        if (isEmpty(text) || ArrayUtils.isEmpty(searchList) || 
ArrayUtils.isEmpty(replacementList)) {
-            return text;
-        }
-
         // if recursing, this shouldn't be less than 0
         if (timeToLive < 0) {
-            throw new IllegalStateException("Aborting to protect against 
StackOverflowError - " +
-                                            "output of one loop is the input 
of another");
+            Set<String> searchSet = new HashSet<>(Arrays.asList(searchList));
+            Set<String> replacementSet = new 
HashSet<>(Arrays.asList(replacementList));
+            searchSet.retainAll(replacementSet);
+            if (searchSet.size() > 0) {
+                throw new IllegalStateException("Aborting to protect against 
StackOverflowError - " +
+                        "output of one loop is the input of another");
+            }
+        }
+
+        if (isEmpty(text) || ArrayUtils.isEmpty(searchList) || 
ArrayUtils.isEmpty(replacementList) || (ArrayUtils.isNotEmpty(searchList) && 
timeToLive == -1)) {
+            return text;
         }
 
         final int searchLength = searchList.length;
diff --git a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java 
b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
index 5c252d7..a83a34b 100644
--- a/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
+++ b/src/test/java/org/apache/commons/lang3/StringUtilsTest.java
@@ -1894,16 +1894,17 @@ public class StringUtilsTest {
     public void testReplace_StringStringArrayStringArrayBoolean() {
         //JAVADOC TESTS START
         assertNull(StringUtils.replaceEachRepeatedly(null, new String[]{"a"}, 
new String[]{"b"}));
-        assertEquals(StringUtils.replaceEachRepeatedly("", new String[]{"a"}, 
new String[]{"b"}), "");
-        assertEquals(StringUtils.replaceEachRepeatedly("aba", null, null), 
"aba");
-        assertEquals(StringUtils.replaceEachRepeatedly("aba", new String[0], 
null), "aba");
-        assertEquals(StringUtils.replaceEachRepeatedly("aba", null, new 
String[0]), "aba");
-        assertEquals(StringUtils.replaceEachRepeatedly("aba", new String[0], 
null), "aba");
-
-        assertEquals(StringUtils.replaceEachRepeatedly("aba", new 
String[]{"a"}, new String[]{""}), "b");
-        assertEquals(StringUtils.replaceEachRepeatedly("aba", new 
String[]{null}, new String[]{"a"}), "aba");
-        assertEquals(StringUtils.replaceEachRepeatedly("abcde", new 
String[]{"ab", "d"}, new String[]{"w", "t"}), "wcte");
-        assertEquals(StringUtils.replaceEachRepeatedly("abcde", new 
String[]{"ab", "d"}, new String[]{"d", "t"}), "tcte");
+        assertEquals("", StringUtils.replaceEachRepeatedly("", new 
String[]{"a"}, new String[]{"b"}));
+        assertEquals("aba", StringUtils.replaceEachRepeatedly("aba", null, 
null));
+        assertEquals("aba", StringUtils.replaceEachRepeatedly("aba", new 
String[0], null));
+        assertEquals("aba", StringUtils.replaceEachRepeatedly("aba", null, new 
String[0]));
+        assertEquals("aba", StringUtils.replaceEachRepeatedly("aba", new 
String[0], null));
+
+        assertEquals("b", StringUtils.replaceEachRepeatedly("aba", new 
String[]{"a"}, new String[]{""}));
+        assertEquals("aba", StringUtils.replaceEachRepeatedly("aba", new 
String[]{null}, new String[]{"a"}));
+        assertEquals("wcte", StringUtils.replaceEachRepeatedly("abcde", new 
String[]{"ab", "d"}, new String[]{"w", "t"}));
+        assertEquals("tcte", StringUtils.replaceEachRepeatedly("abcde", new 
String[]{"ab", "d"}, new String[]{"d", "t"}));
+        assertEquals("blaan", StringUtils.replaceEachRepeatedly("blllaan", new 
String[]{"llaan"}, new String[]{"laan"}) );
 
         assertThrows(
                 IllegalStateException.class,

Reply via email to