diff --git a/server/src/main/java/org/opensearch/common/regex/Regex.java b/server/src/main/java/org/opensearch/common/regex/Regex.java index 3cede72486094..a766226709697 100644 --- a/server/src/main/java/org/opensearch/common/regex/Regex.java +++ b/server/src/main/java/org/opensearch/common/regex/Regex.java @@ -93,23 +93,6 @@ public static Automaton simpleMatchToAutomaton(String... patterns) { return Operations.union(automata); } - /** - * - * @param str - The input string to remove adjacent duplicate characters from - * @param target - The target character to remove duplicates of - * @return - string with adjacent duplicates of the target character removed - */ - static String removeAdjacentDuplicates(String str, char target) { - StringBuilder sb = new StringBuilder(); - for (char c : str.toCharArray()) { - int size = sb.length(); - if (size == 0 || c != target || sb.charAt(size - 1) != c) { - sb.append(c); - } - } - return sb.toString(); - } - /** * Match a String against the given pattern, supporting the following simple * pattern styles: "xxx*", "*xxx", "*xxx*" and "xxx*yyy" matches (with an @@ -142,8 +125,7 @@ public static boolean simpleMatch(String pattern, String str, boolean caseInsens pattern = Strings.toLowercaseAscii(pattern); str = Strings.toLowercaseAscii(str); } - String trimmedPattern = removeAdjacentDuplicates(pattern, '*'); - return simpleMatchWithNormalizedStrings(trimmedPattern, str); + return simpleMatchWithNormalizedStrings(pattern, str); } private static boolean simpleMatchWithNormalizedStrings(String pattern, String str) { @@ -160,7 +142,16 @@ private static boolean simpleMatchWithNormalizedStrings(String pattern, String s // str.endsWith(pattern.substring(1)), but avoiding the construction of pattern.substring(1): return str.regionMatches(str.length() - pattern.length() + 1, pattern, 1, pattern.length() - 1); } else if (nextIndex == 1) { - throw new IllegalArgumentException("Invalid input. Input must not contain adjacent duplicate wildcards."); + // Double wildcard "**" - skipping the first "*" + int skipTillIndex = 1; + for (int i = 1; i < pattern.length(); i++) { + if (pattern.charAt(i) != '*') { + break; + } + skipTillIndex = i; + } + ; + return simpleMatchWithNormalizedStrings(pattern.substring(skipTillIndex), str); } final String part = pattern.substring(1, nextIndex); int partIndex = str.indexOf(part); diff --git a/server/src/test/java/org/opensearch/common/regex/RegexTests.java b/server/src/test/java/org/opensearch/common/regex/RegexTests.java index 6ac156fe15f9c..2cfc2be2987dd 100644 --- a/server/src/test/java/org/opensearch/common/regex/RegexTests.java +++ b/server/src/test/java/org/opensearch/common/regex/RegexTests.java @@ -124,11 +124,11 @@ public void testSimpleMatch() { } } - public void testRemoveDuplicates() { - assertEquals("*", Regex.removeAdjacentDuplicates("***", '*')); - assertEquals("*abc*", Regex.removeAdjacentDuplicates("**abc**", '*')); - assertEquals("a*b*c*", Regex.removeAdjacentDuplicates("a*b**c****", '*')); - assertEquals("*abc", Regex.removeAdjacentDuplicates("****abc", '*')); - assertEquals("*", Regex.removeAdjacentDuplicates("*".repeat(100), '*')); + public void testLargeInput() { + try { + Regex.simpleMatch("*".repeat(10_000), "abc"); + } catch (Error e) { + fail("Received error while evaluating regex: " + e); + } } }