Skip to content

Fix wrong NaN comparison (#61795) #61811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ private static int component(final Edge edge, final int id, final ArrayList<Edge
if (visitedEdge.containsKey(current.coordinate)) {
partitionPoint[0] = current.coordinate.getX();
partitionPoint[1] = current.coordinate.getY();
partitionPoint[2] = current.coordinate.getZ();
if (connectedComponents > 0 && current.next != edge) {
throw new InvalidShapeException("Shape contains more than one shared point");
}
Expand Down Expand Up @@ -576,7 +577,7 @@ private static Point[] coordinates(Edge component, Point[] coordinates, double[]
}
// First and last coordinates must be equal
if (coordinates[0].equals(coordinates[coordinates.length - 1]) == false) {
if (partitionPoint[2] == Double.NaN) {
if (Double.isNaN(partitionPoint[2])) {
throw new InvalidShapeException("Self-intersection at or near point ["
+ partitionPoint[0] + "," + partitionPoint[1] + "]");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ private static Coordinate[] coordinates(Edge component, Coordinate[] coordinates
}
// First and last coordinates must be equal
if (coordinates[0].equals(coordinates[coordinates.length - 1]) == false) {
if (partitionPoint[2] == Double.NaN) {
if (Double.isNaN(partitionPoint[2])) {
throw new InvalidShapeException("Self-intersection at or near point ["
+ partitionPoint[0] + "," + partitionPoint[1] + "]");
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@

package org.elasticsearch.common.geo;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.not;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
Expand All @@ -37,14 +48,7 @@
import org.elasticsearch.geometry.Rectangle;
import org.elasticsearch.index.mapper.GeoShapeIndexer;
import org.elasticsearch.test.ESTestCase;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.Matchers.instanceOf;
import org.locationtech.spatial4j.exception.InvalidShapeException;

public class GeometryIndexerTests extends ESTestCase {

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

public void testInvalidSelfCrossingPolygon() {
Polygon polygon = new Polygon(new LinearRing(
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}
));
Exception e = expectThrows(InvalidShapeException.class, () -> indexer.prepareForIndexing(polygon));
assertThat(e.getMessage(), containsString("Self-intersection at or near point ["));
assertThat(e.getMessage(), not(containsString("NaN")));
}

private XContentBuilder polygon(Boolean orientation, double... val) throws IOException {
XContentBuilder pointGeoJson = XContentFactory.jsonBuilder().startObject();
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import static org.elasticsearch.test.hamcrest.ElasticsearchGeoAssertions.assertMultiPolygon;
import static org.elasticsearch.test.hamcrest.ElasticsearchGeoAssertions.assertPolygon;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.not;

/**
* Tests for {@link ShapeBuilder}
*/
Expand Down Expand Up @@ -775,8 +777,10 @@ public void testInvalidSelfCrossingPolygon() {
);
Exception e = expectThrows(InvalidShapeException.class, () -> builder.close().buildS4J());
assertThat(e.getMessage(), containsString("Self-intersection at or near point ["));
assertThat(e.getMessage(), not(containsString("NaN")));
e = expectThrows(InvalidShapeException.class, () -> buildGeometry(builder.close()));
assertThat(e.getMessage(), containsString("Self-intersection at or near point ["));
assertThat(e.getMessage(), not(containsString("NaN")));
}

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