diff --git a/CHANGELOG.md b/CHANGELOG.md index 1598defbd..61493f65d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ The public API of this library consists of the functions declared in file [h3api.h.in](./src/h3lib/include/h3api.h.in). ## [Unreleased] +### Fixed +- Finding invalid edge boundaries should not crash. (#399) ## [3.7.0] - 2020-09-28 ### Added diff --git a/src/apps/testapps/testH3UniEdge.c b/src/apps/testapps/testH3UniEdge.c index 5af554f8e..68b8c0545 100644 --- a/src/apps/testapps/testH3UniEdge.c +++ b/src/apps/testapps/testH3UniEdge.c @@ -342,4 +342,14 @@ SUITE(h3UniEdge) { "Only one edge was deleted for the pentagon"); } } + + TEST(exactEdgeLength_invalid) { + // Test that invalid inputs do not cause crashes. + t_assert(H3_EXPORT(exactEdgeLengthRads)(0) == 0, + "Invalid edge has zero length"); + GeoCoord zero = {0, 0}; + H3Index h3 = H3_EXPORT(geoToH3)(&zero, 0); + t_assert(H3_EXPORT(exactEdgeLengthRads)(h3) == 0, + "Non-edge (cell) has zero edge length"); + } } diff --git a/src/h3lib/lib/h3UniEdge.c b/src/h3lib/lib/h3UniEdge.c index 9ae5b9e98..32e7e7e17 100644 --- a/src/h3lib/lib/h3UniEdge.c +++ b/src/h3lib/lib/h3UniEdge.c @@ -239,6 +239,12 @@ void H3_EXPORT(getH3UnidirectionalEdgeBoundary)(H3Index edge, GeoBoundary* gb) { // Get the start vertex for the edge int startVertex = vertexNumForDirection(origin, direction); + if (startVertex == INVALID_VERTEX_NUM) { + // This is not actually an edge (i.e. no valid direction), + // so return no vertices. + gb->numVerts = 0; + return; + } // Get the geo boundary for the appropriate vertexes of the origin. Note // that while there are always 2 topological vertexes per edge, the