Skip to content

Commit

Permalink
IndexedFacetDistance: Avoid crash on Inf inputs
Browse files Browse the repository at this point in the history
Fixes #821
  • Loading branch information
dbaston committed Feb 28, 2023
1 parent 52740ec commit aa4503f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changes in 3.9.5

- Bug fixes / improvements:
- GEOSUnaryUnion: Fix crash on collection containing empty point (GH-830, Dan Baston)
- IndexedFacetDistance: Fix crash with Inf coordinates (GH-821, Dan Baston)


Changes in 3.9.4
Expand Down
2 changes: 1 addition & 1 deletion src/operation/distance/FacetSequence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ FacetSequence::computeDistancePointLine(const Coordinate& pt,
const Coordinate& q0 = facetSeq.pts->getAt(i);
const Coordinate& q1 = facetSeq.pts->getAt(i + 1);
double dist = Distance::pointToSegment(pt, q0, q1);
if(dist < minDistance) {
if(dist < minDistance || (locs != nullptr && locs->empty())) {
minDistance = dist;
if (locs != nullptr) {
updateNearestLocationsPointLine(pt, facetSeq, i, q0, q1, locs);
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/operation/distance/IndexedFacetDistanceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#define M_PI 3.14159265358979323846
#endif

using geos::operation::distance::IndexedFacetDistance;

namespace tut {
//
// Test Group
Expand Down Expand Up @@ -366,6 +368,31 @@ void object::test<11>
catch (const GEOSException&) { }
}

// Test with Inf coords
template<>
template<>
void object::test<12>()
{
auto g1 = _wktreader.read("POINT (0 0)");
auto g2 = _wktreader.read("LINESTRING (3 Inf, 5 Inf)");

IndexedFacetDistance ifd1(g1.get());

auto pts = ifd1.nearestPoints(g2.get());
ensure_equals(pts.size(), 2u);

auto seq = geos::detail::make_unique<geos::geom::CoordinateArraySequence>(std::move(pts));

auto ls = _factory->createLineString(std::move(seq));
ls->normalize();

const auto& normPts = *ls->getCoordinatesRO();

ensure_equals(normPts.getX(0), 0);
ensure_equals(normPts.getY(0), 0);
ensure_equals(normPts.getY(1), geos::DoubleInfinity);
}



// TODO: finish the tests by adding:
Expand Down

0 comments on commit aa4503f

Please sign in to comment.