diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index bcf4754f715..222d089fd79 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -19,6 +19,9 @@ Improvements * 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) +* LUCENE-8918: PhraseQuery throws exceptions at construction time if it is passed + null arguments. (Alan Woodward) + Other * LUCENE-8778 LUCENE-8911: Define analyzer SPI names as static final fields and document the names in Javadocs. diff --git a/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java b/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java index c68c73bba74..e477d1c7991 100644 --- a/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java +++ b/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Objects; import java.util.Set; import org.apache.lucene.codecs.lucene50.Lucene50PostingsFormat; @@ -111,6 +112,7 @@ public class PhraseQuery extends Query { * all of them. */ public Builder add(Term term, int position) { + Objects.requireNonNull(term, "Cannot add a null term to PhraseQuery"); if (position < 0) { throw new IllegalArgumentException("Positions must be >= 0, got " + position); } @@ -154,6 +156,9 @@ public class PhraseQuery extends Query { if (slop < 0) { 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) { if (terms[i-1].field().equals(terms[i].field()) == false) { 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) { Term[] terms = new Term[termStrings.length]; 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]); } return terms; @@ -195,6 +201,7 @@ public class PhraseQuery extends Query { private static Term[] toTerms(String field, BytesRef... termBytes) { Term[] terms = new Term[termBytes.length]; 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]); } return terms; diff --git a/lucene/core/src/test/org/apache/lucene/search/TestPhraseQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestPhraseQuery.java index d6529a622f2..5b54870e378 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestPhraseQuery.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestPhraseQuery.java @@ -1071,4 +1071,15 @@ public class TestPhraseQuery extends LuceneTestCase { reader.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()); + } }