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

* Fix integer overflow in GeoEncodingUtils#Grid implementations

* Add entry in CHANGES.txt
This commit is contained in:
Ignacio Vera 2024-09-02 12:13:53 +02:00 committed by GitHub
parent ce4f56e74a
commit 68cc8734ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 43 additions and 2 deletions

View File

@ -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
---------------------

View File

@ -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);

View File

@ -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);
}
}