Skip to content

Apache Lucene open-source search software

unify exception thrown by regexp & check repetition range (#12277)

tang donghai4b3f7662ce88
diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/RegExp.java b/lucene/core/src/java/org/apache/lucene/util/automaton/RegExp.java
index ed1688efd30..0d17a6fcab4 100644
--- a/lucene/core/src/java/org/apache/lucene/util/automaton/RegExp.java
+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/RegExp.java
@@ -1128,6 +1128,10 @@ public class RegExp {
if (start != pos) m = Integer.parseInt(originalString.substring(start, pos));
} else m = n;
if (!match('}')) throw new IllegalArgumentException("expected '}' at position " + pos);
+ if (m != -1 && n > m) {
+ throw new IllegalArgumentException(
+ "invalid repetition range(out of order): " + n + ".." + m);
+ }
if (m == -1) e = makeRepeat(flags, e, n);
else e = makeRepeat(flags, e, n, m);
}
diff --git a/lucene/core/src/test/org/apache/lucene/util/automaton/TestRegExp.java b/lucene/core/src/test/org/apache/lucene/util/automaton/TestRegExp.java
index c960e736304..8f6f765f293 100644
--- a/lucene/core/src/test/org/apache/lucene/util/automaton/TestRegExp.java
+++ b/lucene/core/src/test/org/apache/lucene/util/automaton/TestRegExp.java
@@ -86,6 +86,17 @@ public class TestRegExp extends LuceneTestCase {
}
}
+ public void testParseIllegalRepeatExp() {
+ // out of order
+ IllegalArgumentException expected =
+ expectThrows(
+ IllegalArgumentException.class,
+ () -> {
+ new RegExp("a{99,11}");
+ });
+ assertTrue(expected.getMessage().contains("out of order"));
+ }
+
static String randomDocValue(int minLength) {
String charPalette = "AAAaaaBbbCccc123456 \t";
StringBuilder sb = new StringBuilder();