Apache Lucene open-source search software
Replace assert with IAE in StringsToAutomaton#build if data is not sorted (#12427)
Shubham Chaudhary9cffd4bcc6d6
diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/Automata.java b/lucene/core/src/java/org/apache/lucene/util/automaton/Automata.javaindex 89522781fdb..9ecf748418f 100644--- a/lucene/core/src/java/org/apache/lucene/util/automaton/Automata.java+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/Automata.java@@ -43,8 +43,8 @@ import org.apache.lucene.util.StringHelper;*/public final class Automata {/**- * {@link #makeStringUnion(Collection)} limits terms of this max length to ensure the stack- * doesn't overflow while building, since our algorithm currently relies on recursion.+ * {@link #makeStringUnion(Iterable)} limits terms of this max length to ensure the stack doesn't+ * overflow while building, since our algorithm currently relies on recursion.*/public static final int MAX_STRING_UNION_TERM_LENGTH = 1000;@@ -576,8 +576,8 @@ public final class Automata {* @return An {@link Automaton} accepting all input strings. The resulting automaton is codepoint* based (full unicode codepoints on transitions).*/- public static Automaton makeStringUnion(Collection<BytesRef> utf8Strings) {- if (utf8Strings.isEmpty()) {+ public static Automaton makeStringUnion(Iterable<BytesRef> utf8Strings) {+ if (utf8Strings.iterator().hasNext() == false) {return makeEmpty();} else {return StringsToAutomaton.build(utf8Strings, false);@@ -593,8 +593,8 @@ public final class Automata {* @return An {@link Automaton} accepting all input strings. The resulting automaton is binary* based (UTF-8 encoded byte transition labels).*/- public static Automaton makeBinaryStringUnion(Collection<BytesRef> utf8Strings) {- if (utf8Strings.isEmpty()) {+ public static Automaton makeBinaryStringUnion(Iterable<BytesRef> utf8Strings) {+ if (utf8Strings.iterator().hasNext() == false) {return makeEmpty();} else {return StringsToAutomaton.build(utf8Strings, true);diff --git a/lucene/core/src/java/org/apache/lucene/util/automaton/StringsToAutomaton.java b/lucene/core/src/java/org/apache/lucene/util/automaton/StringsToAutomaton.javaindex 3cfe945789e..58a081fa6a2 100644--- a/lucene/core/src/java/org/apache/lucene/util/automaton/StringsToAutomaton.java+++ b/lucene/core/src/java/org/apache/lucene/util/automaton/StringsToAutomaton.java@@ -18,7 +18,6 @@ package org.apache.lucene.util.automaton;import java.io.IOException;import java.util.Arrays;-import java.util.Collection;import java.util.HashMap;import java.util.IdentityHashMap;import org.apache.lucene.util.ArrayUtil;@@ -35,8 +34,8 @@ import org.apache.lucene.util.UnicodeUtil;* to directly build a binary {@link Automaton} representation. Users should access this* functionality through {@link Automata} static methods.*- * @see Automata#makeStringUnion(Collection)- * @see Automata#makeBinaryStringUnion(Collection)+ * @see Automata#makeStringUnion(Iterable)+ * @see Automata#makeBinaryStringUnion(Iterable)* @see Automata#makeStringUnion(BytesRefIterator)* @see Automata#makeBinaryStringUnion(BytesRefIterator)*/@@ -238,7 +237,7 @@ final class StringsToAutomaton {* UTF-8 codepoints as transition labels or binary (compiled) transition labels based on {@code* asBinary}.*/- static Automaton build(Collection<BytesRef> input, boolean asBinary) {+ static Automaton build(Iterable<BytesRef> input, boolean asBinary) {final StringsToAutomaton builder = new StringsToAutomaton();for (BytesRef b : input) {@@ -273,9 +272,11 @@ final class StringsToAutomaton {+ current);}assert stateRegistry != null : "Automaton already built.";- assert previous == null || previous.get().compareTo(current) <= 0- : "Input must be in sorted UTF-8 order: " + previous.get() + " >= " + current;- assert setPrevious(current);+ if (previous != null && previous.get().compareTo(current) > 0) {+ throw new IllegalArgumentException(+ "Input must be in sorted UTF-8 order: " + previous.get() + " >= " + current);+ }+ setPrevious(current);// Reusable codepoint information if we're building a non-binary based automatonUnicodeUtil.UTF8CodePoint codePoint = null;