LUCENE-8918: Disallow null terms in PhraseQuery

This commit is contained in:
Alan Woodward 2019-07-18 10:10:36 +01:00
parent 241c44a82d
commit ee09208293
3 changed files with 21 additions and 0 deletions

View File

@ -19,6 +19,9 @@ Improvements
* LUCENE-8914: move the logic for discarding inner modes to the IntersectVisitor so we take advantage * LUCENE-8914: move the logic for discarding inner modes to the IntersectVisitor so we take advantage
of the change introduced in LUCENE-7862. (Ignacio Vera) of the change introduced in LUCENE-7862. (Ignacio Vera)
* LUCENE-8918: PhraseQuery throws exceptions at construction time if it is passed
null arguments. (Alan Woodward)
Other Other
* LUCENE-8778 LUCENE-8911: Define analyzer SPI names as static final fields and document the names in Javadocs. * LUCENE-8778 LUCENE-8911: Define analyzer SPI names as static final fields and document the names in Javadocs.

View File

@ -22,6 +22,7 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Set; import java.util.Set;
import org.apache.lucene.codecs.lucene50.Lucene50PostingsFormat; import org.apache.lucene.codecs.lucene50.Lucene50PostingsFormat;
@ -111,6 +112,7 @@ public class PhraseQuery extends Query {
* all of them. * all of them.
*/ */
public Builder add(Term term, int position) { public Builder add(Term term, int position) {
Objects.requireNonNull(term, "Cannot add a null term to PhraseQuery");
if (position < 0) { if (position < 0) {
throw new IllegalArgumentException("Positions must be >= 0, got " + position); throw new IllegalArgumentException("Positions must be >= 0, got " + position);
} }
@ -154,6 +156,9 @@ public class PhraseQuery extends Query {
if (slop < 0) { if (slop < 0) {
throw new IllegalArgumentException("Slop must be >= 0, got " + slop); throw new IllegalArgumentException("Slop must be >= 0, got " + slop);
} }
for (Term term : terms) {
Objects.requireNonNull(term, "Cannot add a null term to PhraseQuery");
}
for (int i = 1; i < terms.length; ++i) { for (int i = 1; i < terms.length; ++i) {
if (terms[i-1].field().equals(terms[i].field()) == false) { if (terms[i-1].field().equals(terms[i].field()) == false) {
throw new IllegalArgumentException("All terms should have the same field"); throw new IllegalArgumentException("All terms should have the same field");
@ -187,6 +192,7 @@ public class PhraseQuery extends Query {
private static Term[] toTerms(String field, String... termStrings) { private static Term[] toTerms(String field, String... termStrings) {
Term[] terms = new Term[termStrings.length]; Term[] terms = new Term[termStrings.length];
for (int i = 0; i < terms.length; ++i) { for (int i = 0; i < terms.length; ++i) {
Objects.requireNonNull(termStrings[i], "Cannot add a null term to PhraseQuery");
terms[i] = new Term(field, termStrings[i]); terms[i] = new Term(field, termStrings[i]);
} }
return terms; return terms;
@ -195,6 +201,7 @@ public class PhraseQuery extends Query {
private static Term[] toTerms(String field, BytesRef... termBytes) { private static Term[] toTerms(String field, BytesRef... termBytes) {
Term[] terms = new Term[termBytes.length]; Term[] terms = new Term[termBytes.length];
for (int i = 0; i < terms.length; ++i) { for (int i = 0; i < terms.length; ++i) {
Objects.requireNonNull(termBytes[i], "Cannot add a null term to PhraseQuery");
terms[i] = new Term(field, termBytes[i]); terms[i] = new Term(field, termBytes[i]);
} }
return terms; return terms;

View File

@ -1071,4 +1071,15 @@ public class TestPhraseQuery extends LuceneTestCase {
reader.close(); reader.close();
dir.close(); dir.close();
} }
public void testNullTerm() {
NullPointerException e = expectThrows(NullPointerException.class, () -> new PhraseQuery.Builder().add(null));
assertEquals("Cannot add a null term to PhraseQuery", e.getMessage());
e = expectThrows(NullPointerException.class, () -> new PhraseQuery("field", (BytesRef)null));
assertEquals("Cannot add a null term to PhraseQuery", e.getMessage());
e = expectThrows(NullPointerException.class, () -> new PhraseQuery("field", (String)null));
assertEquals("Cannot add a null term to PhraseQuery", e.getMessage());
}
} }