Skip to content

Commit

Permalink
TopologyPreservingSimplifier: Avoid stack overflow on degenerate input
Browse files Browse the repository at this point in the history
Fixes #1070
  • Loading branch information
dbaston committed Jun 19, 2024
1 parent f03a3f3 commit a02617f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/simplify/TaggedLineStringSimplifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ TaggedLineStringSimplifier::simplifySection(std::size_t i,
<< std::endl;
#endif

if (distance < 0) {
// negative distance indicates that we could not compute distance to the
// farthest point, probably because of infinite or large-magnitude coordinates.
// avoid trying to simplify this section.
for (std::size_t k = i; k < j; k++) {
auto newSeg = std::make_unique<TaggedLineSegment>(*(line->getSegment(k)));
line->addToResult(std::move(newSeg));
}

return;
}

// flattening must be less than distanceTolerance
if(distance > distanceTolerance) {
isValidToSimplify = false;
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/simplify/TopologyPreservingSimplifierTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,5 +430,17 @@ void object::test<33>()
"POLYGON ((-222618.41756525903 6299886.966175825, -222510 6300300, -221720.85158014414 6300132.680680807, -222448.77936063593 6299647.669870703, -222618.41756525903 6299886.966175825), (-222467.0202338936 6299970.185860572, -222456.57057590774 6299931.950053182, -222490.56062790897 6299837.359974515, -222415.1394852571 6299926.6972160805, -222421.19226152284 6299963.389132584, -222467.0202338936 6299970.185860572))");
}

// https://github.com/libgeos/geos/issues/1070
template<>
template<>
void object::test<34>()
{
auto input = wktreader.read("MULTILINESTRING((0 0, 1 0001211111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111, 1 00015111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111, 1 0001511111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111, 1 0, 10 01, 0 0))");

auto simplified = TopologyPreservingSimplifier::simplify(input.get(), 2.0);

ensure(simplified != nullptr); // no crash
}


} // namespace tut

0 comments on commit a02617f

Please sign in to comment.