Skip to content

Commit

Permalink
IndexedFacetDistance: Avoid crash with Inf coordinates
Browse files Browse the repository at this point in the history
Fixes #821
  • Loading branch information
dbaston committed Feb 27, 2023
1 parent 625297b commit d1adb8f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
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
2 changes: 2 additions & 0 deletions tests/unit/algorithm/construct/MaximumInscribedCircleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <geos/geom/PrecisionModel.h>
#include <geos/io/WKTReader.h>
#include <geos/io/WKTWriter.h>
#include <geos/io/WKBReader.h>
// std
#include <sstream>
#include <string>
Expand All @@ -37,6 +38,7 @@ struct test_mic_data {
geos::geom::GeometryFactory::Ptr factory_;
geos::io::WKTReader reader_;
geos::io::WKTWriter writer_;
geos::io::WKBReader wkbreader_;

test_mic_data():
geom_(nullptr),
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/capi/GEOSMaximumInscribedCircleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,22 @@ void object::test<2>
}


// Crash with Inf coords
// https://github.com/libgeos/geos/issues/821
template<>
template<>
void object::test<3>
()
{
std::string wkb("0106000020E61000000100000001030000000100000005000000000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F000000000000F07F");
geom1_ = GEOSGeomFromHEX_buf(reinterpret_cast<const unsigned char*>(wkb.c_str()), wkb.size());

result_ = GEOSMaximumInscribedCircle(geom1_, 1);

// no crash
}



} // namespace tut

16 changes: 16 additions & 0 deletions tests/unit/operation/distance/IndexedFacetDistanceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#define M_PI 3.14159265358979323846
#endif

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

namespace tut {
//
// Test Group
Expand Down Expand Up @@ -382,6 +384,20 @@ 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);
}



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

0 comments on commit d1adb8f

Please sign in to comment.