Skip to content

Apache Lucene open-source search software

Fix failure found by TestOperations.testGetRandomAcceptedString (#14227)

Robert Muir19be0e5fd11a
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 aaf50c8c180..8be1a0eb12c 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
@@ -1243,7 +1243,15 @@ public class RegExp {
do {
// look for escape
if (match('\\')) {
- expandPreDefined(starts, ends);
+ if (peek("\\ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")) {
+ // special "escape" or invalid escape
+ expandPreDefined(starts, ends);
+ } else {
+ // escaped character, don't parse it
+ int c = next();
+ starts.add(c);
+ ends.add(c);
+ }
} else {
// parse a character
int c = parseCharExp();
diff --git a/lucene/core/src/test/org/apache/lucene/util/automaton/TestOperations.java b/lucene/core/src/test/org/apache/lucene/util/automaton/TestOperations.java
index 7fb1d679946..aedac499ecc 100644
--- a/lucene/core/src/test/org/apache/lucene/util/automaton/TestOperations.java
+++ b/lucene/core/src/test/org/apache/lucene/util/automaton/TestOperations.java
@@ -132,10 +132,11 @@ public class TestOperations extends LuceneTestCase {
final int ITER2 = atLeast(100);
for (int i = 0; i < ITER1; i++) {
- final RegExp re = new RegExp(AutomatonTestUtil.randomRegexp(random()), RegExp.NONE);
+ final String text = AutomatonTestUtil.randomRegexp(random());
+ final RegExp re = new RegExp(text, RegExp.NONE);
// System.out.println("TEST i=" + i + " re=" + re);
final Automaton a = Operations.determinize(re.toAutomaton(), DEFAULT_DETERMINIZE_WORK_LIMIT);
- assertFalse(Operations.isEmpty(a));
+ assertFalse("empty: " + text, Operations.isEmpty(a));
final AutomatonTestUtil.RandomAcceptedStrings rx =
new AutomatonTestUtil.RandomAcceptedStrings(a);
diff --git a/lucene/core/src/test/org/apache/lucene/util/automaton/TestRegExpParsing.java b/lucene/core/src/test/org/apache/lucene/util/automaton/TestRegExpParsing.java
index 51a14761d38..c5e269b51f9 100644
--- a/lucene/core/src/test/org/apache/lucene/util/automaton/TestRegExpParsing.java
+++ b/lucene/core/src/test/org/apache/lucene/util/automaton/TestRegExpParsing.java
@@ -379,6 +379,15 @@ public class TestRegExpParsing extends LuceneTestCase {
assertSameLanguage(expected, actual);
}
+ public void testEscapedDashCharClass() {
+ RegExp re = new RegExp("[\\-]");
+ assertEquals("REGEXP_CHAR char=-\n", re.toStringTree());
+ Automaton actual = re.toAutomaton();
+ assertTrue(actual.isDeterministic());
+ Automaton expected = Automata.makeChar('-');
+ assertSameLanguage(expected, actual);
+ }
+
public void testEmpty() {
RegExp re = new RegExp("#", RegExp.EMPTY);
assertEquals("#", re.toString());
@@ -391,6 +400,26 @@ public class TestRegExpParsing extends LuceneTestCase {
assertSameLanguage(expected, actual);
}
+ public void testEmptyClass() {
+ Exception expected =
+ expectThrows(
+ IllegalArgumentException.class,
+ () -> {
+ new RegExp("[]");
+ });
+ assertEquals("expected ']' at position 2", expected.getMessage());
+ }
+
+ public void testEscapedInvalidClass() {
+ Exception expected =
+ expectThrows(
+ IllegalArgumentException.class,
+ () -> {
+ new RegExp("[\\]");
+ });
+ assertEquals("expected ']' at position 3", expected.getMessage());
+ }
+
public void testInterval() {
RegExp re = new RegExp("<5-40>");
assertEquals("<5-40>", re.toString());