Skip to content

Apache Lucene open-source search software

Add missing bounds check to PointRangeQuery for inclusive arrays.

Robert Muird21fe2e34a74
diff --git a/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java b/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java
index c68715dfdfe..59f8f0fd775 100644
--- a/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/PointRangeQuery.java
@@ -87,6 +87,12 @@ public abstract class PointRangeQuery extends Query {
if (upperPoint.length != numDims) {
throw new IllegalArgumentException("lowerPoint has length=" + numDims + " but upperPoint has different length=" + upperPoint.length);
}
+ if (lowerInclusive.length != numDims) {
+ throw new IllegalArgumentException("lowerInclusive has length=" + lowerInclusive.length + " but expected=" + numDims);
+ }
+ if (upperInclusive.length != numDims) {
+ throw new IllegalArgumentException("upperInclusive has length=" + upperInclusive.length + " but expected=" + numDims);
+ }
this.lowerPoint = lowerPoint;
this.lowerInclusive = lowerInclusive;
this.upperPoint = upperPoint;
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java b/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java
index 4a232a650b6..38d46e1db90 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java
@@ -1068,6 +1068,33 @@ public class TestPointQueries extends LuceneTestCase {
IOUtils.close(r, w, dir);
}
+ /** ensure good exception when boolean[]s for inclusive have wrong length */
+ public void testWrongNumBooleans() throws Exception {
+ Directory dir = newDirectory();
+ IndexWriterConfig iwc = newIndexWriterConfig();
+ iwc.setCodec(getCodec());
+ RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
+ Document doc = new Document();
+ doc.add(new LongPoint("value", 1L, 2L));
+ w.addDocument(doc);
+
+ IndexReader r = w.getReader();
+
+ // no wrapping, else the exc might happen in executor thread:
+ IndexSearcher s = new IndexSearcher(r);
+ IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
+ s.count(LongPoint.newMultiRangeQuery("value", new Long[] { 1L, 2L }, new boolean[] {true}, new Long[] { 1L, 2L }, new boolean[] {true, true}));
+ });
+ assertEquals("lowerInclusive has length=1 but expected=2", expected.getMessage());
+
+ expected = expectThrows(IllegalArgumentException.class, () -> {
+ s.count(LongPoint.newMultiRangeQuery("value", new Long[] { 1L, 2L }, new boolean[] {true, true}, new Long[] { 1L, 2L }, new boolean[] {true}));
+ });
+ assertEquals("upperInclusive has length=1 but expected=2", expected.getMessage());
+
+ IOUtils.close(r, w, dir);
+ }
+
public void testWrongNumBytes() throws Exception {
Directory dir = newDirectory();
IndexWriterConfig iwc = newIndexWriterConfig();