Skip to content

Commit

Permalink
Fix integer overflow in GeoEncodingUtils#Grid implementations (apache…
Browse files Browse the repository at this point in the history
…#13704)

* Fix integer overflow in GeoEncodingUtils#Grid implementations

* Add entry in CHANGES.txt
  • Loading branch information
iverase authored Sep 2, 2024
1 parent ce4f56e commit 68cc873
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ private DistancePredicate(
*/
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);
Expand Down Expand Up @@ -411,7 +411,7 @@ private Component2DPredicate(
*/
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
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;
Expand Down Expand Up @@ -1751,4 +1752,41 @@ public void testSmallSetDistanceDateline() throws Exception {
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);
}
}

0 comments on commit 68cc873

Please sign in to comment.