Skip to content

Commit 3db005a

Browse files
authored
LineIntersector: Fix invalid read on mixed-dimensionality inputs (#1115)
1 parent d84cf1a commit 3db005a

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

include/geos/algorithm/LineIntersector.h

+14-2
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,20 @@ class GEOS_DLL LineIntersector {
589589
{
590590
geom::CoordinateXYZM ptInt(Intersection::intersection(p1, p2, q1, q2));
591591
if (ptInt.isNull()) {
592-
// FIXME need to cast to correct type in mixed-dimensionality case
593-
ptInt = static_cast<const C1&>(nearestEndpoint(p1, p2, q1, q2));
592+
const geom::CoordinateXY& nearest = nearestEndpoint(p1, p2, q1, q2);
593+
#if __cplusplus >= 201703L
594+
if constexpr (std::is_same<C1, C2>::value) {
595+
#else
596+
if (std::is_same<C1, C2>::value) {
597+
#endif
598+
ptInt = static_cast<const C1&>(nearest);
599+
} else {
600+
if (&nearest == static_cast<const geom::CoordinateXY*>(&p1) || &nearest == static_cast<const geom::CoordinateXY*>(&p2)) {
601+
ptInt = static_cast<const C1&>(nearest);
602+
} else {
603+
ptInt = static_cast<const C2&>(nearest);
604+
}
605+
}
594606
}
595607
return ptInt;
596608
}

tests/unit/capi/GEOSIntersectionTest.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -157,5 +157,21 @@ void object::test<8>()
157157
ensure("curved geometry not supported", result_ == nullptr);
158158
}
159159

160+
// https://github.com/libgeos/geos/issues/1074
161+
// Make sure no crash occurs when LineIntersector has mixed-dimensionality
162+
// inputs and substitutes a segment endpoint for intersection point
163+
template<>
164+
template<>
165+
void object::test<9>()
166+
{
167+
geom1_ = fromWKT("GEOMETRYCOLLECTION(POINT(0 0 59.083333333333336), POINT(1 1 59.083333333333336), LINESTRING(2 4 111 1, 2 223 22 2), POLYGON((42 -2.222222223222222e+155 111 1, 2 3 1 1, 2 4 111 NaN, 42 -2.222222223222222e+155 111 NaN)), POLYGON((2.2222222212222224e+149 4 111 1, 42 -2.222222222222222e+149 22 2, 2 4 111 1, 22 2222222222222223 22 NaN, 2.2222222212222224e+149 4 111 NaN)))");
168+
geom2_ = fromWKT("LINESTRING (1.6409301755752343e+149 -5.298190649085575e+148, 1.6666666660752404e+149 -5.55555555396982e+148)");
169+
170+
result_ = GEOSIntersection(geom1_, geom2_);
171+
ensure(result_ != nullptr);
172+
ensure("HasZ", GEOSHasZ(result_));
173+
ensure("HasM", GEOSHasM(result_));
174+
}
175+
160176
} // namespace tut
161177

0 commit comments

Comments
 (0)