You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ro...@apache.org on 2019/07/18 12:40:00 UTC
[lucene-solr] branch master updated: LUCENE-8918: Disallow null
terms in PhraseQuery
This is an automated email from the ASF dual-hosted git repository.
romseygeek pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git
The following commit(s) were added to refs/heads/master by this push:
new 15b08f9 LUCENE-8918: Disallow null terms in PhraseQuery
15b08f9 is described below
commit 15b08f9a065cf58ad77c6456b7a469e3b12404b2
Author: Alan Woodward <ro...@apache.org>
AuthorDate: Thu Jul 18 10:10:36 2019 +0100
LUCENE-8918: Disallow null terms in PhraseQuery
---
lucene/CHANGES.txt | 3 +++
.../core/src/java/org/apache/lucene/search/PhraseQuery.java | 7 +++++++
.../src/test/org/apache/lucene/search/TestPhraseQuery.java | 11 +++++++++++
3 files changed, 21 insertions(+)
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index f50a578..5a3eb83 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -65,6 +65,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 6c6460d..fcb8e1c 100644
--- a/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/PhraseQuery.java
@@ -21,6 +21,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import java.util.Objects;
import org.apache.lucene.codecs.lucene50.Lucene50PostingsFormat;
import org.apache.lucene.codecs.lucene50.Lucene50PostingsReader;
@@ -109,6 +110,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);
}
@@ -152,6 +154,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");
@@ -185,6 +190,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;
@@ -193,6 +199,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 d6529a6..5b54870 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());
+ }
}