Skip to content

Apache Lucene open-source search software

Fix integer overflow in GeoEncodingUtils#Grid implementations (#13704)

Ignacio Vera68cc8734ca28
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index ae15247ac23..63390dd8f9f 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -410,6 +410,9 @@ Bug Fixes
* GITHUB#13691: Fix incorrect exponent value in explain of SigmoidFunction. (Owais Kazi)
+* GITHUB#13703: Fix bug in LatLonPoint queries where narrow polygons close to latitude 90 don't
+ match any points due to an Integer overflow. (Ignacio Vera)
+
Build
---------------------
diff --git a/lucene/core/src/java/org/apache/lucene/geo/GeoEncodingUtils.java b/lucene/core/src/java/org/apache/lucene/geo/GeoEncodingUtils.java
index d7ff62ba3be..818639bee8f 100644
--- a/lucene/core/src/java/org/apache/lucene/geo/GeoEncodingUtils.java
+++ b/lucene/core/src/java/org/apache/lucene/geo/GeoEncodingUtils.java
@@ -363,7 +363,7 @@ public final class GeoEncodingUtils {
*/
public boolean test(int lat, int lon) {
final int lat2 = ((lat - Integer.MIN_VALUE) >>> latShift);
- if (lat2 < latBase || lat2 >= latBase + maxLatDelta) {
+ if (lat2 < latBase || lat2 - latBase >= maxLatDelta) {
return false;
}
int lon2 = ((lon - Integer.MIN_VALUE) >>> lonShift);
@@ -411,7 +411,7 @@ public final class GeoEncodingUtils {
*/
public boolean test(int lat, int lon) {
final int lat2 = ((lat - Integer.MIN_VALUE) >>> latShift);
- if (lat2 < latBase || lat2 >= latBase + maxLatDelta) {
+ if (lat2 < latBase || lat2 - latBase >= maxLatDelta) {
return false;
}
int lon2 = ((lon - Integer.MIN_VALUE) >>> lonShift);
diff --git a/lucene/test-framework/src/java/org/apache/lucene/tests/geo/BaseGeoPointTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/tests/geo/BaseGeoPointTestCase.java
index d371292324d..5c0c134fef3 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/tests/geo/BaseGeoPointTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/tests/geo/BaseGeoPointTestCase.java
@@ -39,6 +39,7 @@ import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.geo.Circle;
import org.apache.lucene.geo.Component2D;
+import org.apache.lucene.geo.GeoEncodingUtils;
import org.apache.lucene.geo.GeoUtils;
import org.apache.lucene.geo.LatLonGeometry;
import org.apache.lucene.geo.Polygon;
@@ -1751,4 +1752,41 @@ public abstract class BaseGeoPointTestCase extends LuceneTestCase {
newDistanceQuery("point", 32.94823588839368, -179.9538113027811, 120000), 20);
assertEquals(3, td.totalHits.value);
}
+
+ public void testNarrowPolygonCloseToNorthPole() throws Exception {
+ IndexWriterConfig iwc = newIndexWriterConfig();
+ iwc.setMergeScheduler(new SerialMergeScheduler());
+ Directory dir = newDirectory();
+ IndexWriter w = new IndexWriter(dir, iwc);
+
+ // index point closes to Lat 90
+ Document doc = new Document();
+ final int base = Integer.MAX_VALUE;
+ addPointToDoc(
+ FIELD_NAME,
+ doc,
+ GeoEncodingUtils.decodeLatitude(base - 2),
+ GeoEncodingUtils.decodeLongitude(base - 2));
+ w.addDocument(doc);
+ w.flush();
+
+ // query testing
+ final IndexReader reader = DirectoryReader.open(w);
+ final IndexSearcher s = newSearcher(reader);
+
+ double minLat = GeoEncodingUtils.decodeLatitude(base - 3);
+ double maxLat = GeoEncodingUtils.decodeLatitude(base);
+ double minLon = GeoEncodingUtils.decodeLongitude(base - 3);
+ double maxLon = GeoEncodingUtils.decodeLongitude(base);
+
+ Query query =
+ newPolygonQuery(
+ FIELD_NAME,
+ new Polygon(
+ new double[] {minLat, minLat, maxLat, maxLat, minLat},
+ new double[] {minLon, maxLon, maxLon, minLon, minLon}));
+
+ assertEquals(1, s.count(query));
+ IOUtils.close(w, reader, dir);
+ }
}