[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_ehuaynalaya@uolinc.com>
This commit is contained in:
Edwin Delgado H 2020-06-13 12:31:07 -03:00 committed by GitHub
parent 995dba307c
commit d62d4e21ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 15 deletions

View File

@ -25,6 +25,8 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import java.util.HashSet;
import java.util.Set;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -6684,14 +6686,19 @@ public class StringUtils {
// mchyzer Performance note: This creates very few new objects (one major goal) // 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 // 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 recursing, this shouldn't be less than 0
if (timeToLive < 0) { if (timeToLive < 0) {
throw new IllegalStateException("Aborting to protect against StackOverflowError - " + Set<String> searchSet = new HashSet<>(Arrays.asList(searchList));
"output of one loop is the input of another"); 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; final int searchLength = searchList.length;

View File

@ -1894,16 +1894,17 @@ public class StringUtilsTest {
public void testReplace_StringStringArrayStringArrayBoolean() { public void testReplace_StringStringArrayStringArrayBoolean() {
//JAVADOC TESTS START //JAVADOC TESTS START
assertNull(StringUtils.replaceEachRepeatedly(null, new String[]{"a"}, new String[]{"b"})); assertNull(StringUtils.replaceEachRepeatedly(null, new String[]{"a"}, new String[]{"b"}));
assertEquals(StringUtils.replaceEachRepeatedly("", new String[]{"a"}, new String[]{"b"}), ""); assertEquals("", StringUtils.replaceEachRepeatedly("", new String[]{"a"}, new String[]{"b"}));
assertEquals(StringUtils.replaceEachRepeatedly("aba", null, null), "aba"); assertEquals("aba", StringUtils.replaceEachRepeatedly("aba", null, null));
assertEquals(StringUtils.replaceEachRepeatedly("aba", new String[0], null), "aba"); assertEquals("aba", StringUtils.replaceEachRepeatedly("aba", new String[0], null));
assertEquals(StringUtils.replaceEachRepeatedly("aba", null, new String[0]), "aba"); assertEquals("aba", StringUtils.replaceEachRepeatedly("aba", null, new String[0]));
assertEquals(StringUtils.replaceEachRepeatedly("aba", new String[0], null), "aba"); assertEquals("aba", StringUtils.replaceEachRepeatedly("aba", new String[0], null));
assertEquals(StringUtils.replaceEachRepeatedly("aba", new String[]{"a"}, new String[]{""}), "b"); assertEquals("b", StringUtils.replaceEachRepeatedly("aba", new String[]{"a"}, new String[]{""}));
assertEquals(StringUtils.replaceEachRepeatedly("aba", new String[]{null}, new String[]{"a"}), "aba"); assertEquals("aba", StringUtils.replaceEachRepeatedly("aba", new String[]{null}, new String[]{"a"}));
assertEquals(StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}), "wcte"); assertEquals("wcte", StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"w", "t"}));
assertEquals(StringUtils.replaceEachRepeatedly("abcde", new String[]{"ab", "d"}, new String[]{"d", "t"}), "tcte"); 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( assertThrows(
IllegalStateException.class, IllegalStateException.class,