Skip to content

Apache Lucene open-source search software

LUCENE-8034: use subtraction instead of addition to sidestep int overflow in SpanNotQuery

Mike McCandless726ee05240a6
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index a77ea34eb44..c7e0995da39 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -107,6 +107,9 @@ Bug Fixes
* LUCENE-8045: ParallelLeafReader did not correctly report FieldInfo.dvGen
(Alan Woodward)
+* LUCENE-8034: Use subtraction instead of addition to sidestep int
+ overflow in SpanNotQuery. (Hari Menon via Mike McCandless)
+
Optimizations
* LUCENE-8018: Smaller FieldInfos memory footprint by not retaining unnecessary
diff --git a/lucene/core/src/java/org/apache/lucene/search/spans/SpanNotQuery.java b/lucene/core/src/java/org/apache/lucene/search/spans/SpanNotQuery.java
index b169bd8119f..d8c7862206a 100644
--- a/lucene/core/src/java/org/apache/lucene/search/spans/SpanNotQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/spans/SpanNotQuery.java
@@ -178,7 +178,7 @@ public final class SpanNotQuery extends SpanQuery {
}
// exclude end position far enough in current doc, check start position:
- if (candidate.endPosition() + post <= excludeSpans.startPosition()) {
+ if (excludeSpans.startPosition() - post >= candidate.endPosition()) {
return AcceptStatus.YES;
} else {
return AcceptStatus.NO;
diff --git a/lucene/core/src/test/org/apache/lucene/search/spans/TestBasics.java b/lucene/core/src/test/org/apache/lucene/search/spans/TestBasics.java
index d699719e478..1941baebbb1 100644
--- a/lucene/core/src/test/org/apache/lucene/search/spans/TestBasics.java
+++ b/lucene/core/src/test/org/apache/lucene/search/spans/TestBasics.java
@@ -196,7 +196,15 @@ public class TestBasics extends LuceneTestCase {
assertTrue(searcher.explain(query, 801).getValue() > 0.0f);
assertTrue(searcher.explain(query, 891).getValue() > 0.0f);
}
-
+
+ public void testSpanNotNoOverflowOnLargeSpans() throws Exception {
+ SpanQuery near = spanNearOrderedQuery("field", 4, "eight", "one");
+ SpanQuery query = spanNotQuery(near, spanTermQuery("field", "forty"), Integer.MAX_VALUE, Integer.MAX_VALUE);
+
+ checkHits(query, new int[]
+ {801, 821, 831, 851, 861, 871, 881, 891, 1801, 1821, 1831, 1851, 1861, 1871, 1881, 1891});
+ }
+
public void testSpanWithMultipleNotSingle() throws Exception {
SpanQuery near = spanNearOrderedQuery("field", 4, "eight", "one");
SpanQuery or = spanOrQuery("field", "forty");