Skip to content

Commit 48e53cc

Browse files
Fix wrong NaN comparison (#61795) (#61811)
Fixes wrong NaN comparison in error message generator in GeoPolygonDecomposer and PolygonBuilder. Supersedes #48207 Co-authored-by: Pedro Luiz Cabral Salomon Prado <[email protected]>
1 parent 8613bde commit 48e53cc

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

server/src/main/java/org/elasticsearch/common/geo/GeoPolygonDecomposer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,7 @@ private static int component(final Edge edge, final int id, final ArrayList<Edge
528528
if (visitedEdge.containsKey(current.coordinate)) {
529529
partitionPoint[0] = current.coordinate.getX();
530530
partitionPoint[1] = current.coordinate.getY();
531+
partitionPoint[2] = current.coordinate.getZ();
531532
if (connectedComponents > 0 && current.next != edge) {
532533
throw new InvalidShapeException("Shape contains more than one shared point");
533534
}
@@ -576,7 +577,7 @@ private static Point[] coordinates(Edge component, Point[] coordinates, double[]
576577
}
577578
// First and last coordinates must be equal
578579
if (coordinates[0].equals(coordinates[coordinates.length - 1]) == false) {
579-
if (partitionPoint[2] == Double.NaN) {
580+
if (Double.isNaN(partitionPoint[2])) {
580581
throw new InvalidShapeException("Self-intersection at or near point ["
581582
+ partitionPoint[0] + "," + partitionPoint[1] + "]");
582583
} else {

server/src/main/java/org/elasticsearch/common/geo/builders/PolygonBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ private static Coordinate[] coordinates(Edge component, Coordinate[] coordinates
436436
}
437437
// First and last coordinates must be equal
438438
if (coordinates[0].equals(coordinates[coordinates.length - 1]) == false) {
439-
if (partitionPoint[2] == Double.NaN) {
439+
if (Double.isNaN(partitionPoint[2])) {
440440
throw new InvalidShapeException("Self-intersection at or near point ["
441441
+ partitionPoint[0] + "," + partitionPoint[1] + "]");
442442
} else {

server/src/test/java/org/elasticsearch/common/geo/GeometryIndexerTests.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@
1919

2020
package org.elasticsearch.common.geo;
2121

22+
import static org.hamcrest.Matchers.containsString;
23+
import static org.hamcrest.Matchers.instanceOf;
24+
import static org.hamcrest.Matchers.not;
25+
26+
import java.io.IOException;
27+
import java.text.ParseException;
28+
import java.util.ArrayList;
29+
import java.util.Arrays;
30+
import java.util.Collections;
31+
import java.util.List;
32+
2233
import org.apache.lucene.index.IndexableField;
2334
import org.elasticsearch.common.xcontent.XContentBuilder;
2435
import org.elasticsearch.common.xcontent.XContentFactory;
@@ -37,14 +48,7 @@
3748
import org.elasticsearch.geometry.Rectangle;
3849
import org.elasticsearch.index.mapper.GeoShapeIndexer;
3950
import org.elasticsearch.test.ESTestCase;
40-
import java.io.IOException;
41-
import java.text.ParseException;
42-
import java.util.ArrayList;
43-
import java.util.Arrays;
44-
import java.util.Collections;
45-
import java.util.List;
46-
47-
import static org.hamcrest.Matchers.instanceOf;
51+
import org.locationtech.spatial4j.exception.InvalidShapeException;
4852

4953
public class GeometryIndexerTests extends ESTestCase {
5054

@@ -357,6 +361,15 @@ public void testPolygonOrientation() throws IOException, ParseException {
357361
actual(polygon(randomBoolean() ? null : randomBoolean(), 20, 0, 20, 10, -20, 10, -20, 0, 20, 0), randomBoolean()));
358362
}
359363

364+
public void testInvalidSelfCrossingPolygon() {
365+
Polygon polygon = new Polygon(new LinearRing(
366+
new double[]{0, 0, 1, 0.5, 1.5, 1, 2, 2, 0}, new double[]{0, 2, 1.9, 1.8, 1.8, 1.9, 2, 0, 0}
367+
));
368+
Exception e = expectThrows(InvalidShapeException.class, () -> indexer.prepareForIndexing(polygon));
369+
assertThat(e.getMessage(), containsString("Self-intersection at or near point ["));
370+
assertThat(e.getMessage(), not(containsString("NaN")));
371+
}
372+
360373
private XContentBuilder polygon(Boolean orientation, double... val) throws IOException {
361374
XContentBuilder pointGeoJson = XContentFactory.jsonBuilder().startObject();
362375
{

server/src/test/java/org/elasticsearch/common/geo/ShapeBuilderTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import static org.elasticsearch.test.hamcrest.ElasticsearchGeoAssertions.assertMultiPolygon;
4444
import static org.elasticsearch.test.hamcrest.ElasticsearchGeoAssertions.assertPolygon;
4545
import static org.hamcrest.Matchers.containsString;
46+
import static org.hamcrest.Matchers.not;
47+
4648
/**
4749
* Tests for {@link ShapeBuilder}
4850
*/
@@ -775,8 +777,10 @@ public void testInvalidSelfCrossingPolygon() {
775777
);
776778
Exception e = expectThrows(InvalidShapeException.class, () -> builder.close().buildS4J());
777779
assertThat(e.getMessage(), containsString("Self-intersection at or near point ["));
780+
assertThat(e.getMessage(), not(containsString("NaN")));
778781
e = expectThrows(InvalidShapeException.class, () -> buildGeometry(builder.close()));
779782
assertThat(e.getMessage(), containsString("Self-intersection at or near point ["));
783+
assertThat(e.getMessage(), not(containsString("NaN")));
780784
}
781785

782786
public Object buildGeometry(ShapeBuilder<?, ?, ?> builder) {

0 commit comments

Comments
 (0)