From e4a9e5db575c818f9ccd0c4e23237ced5b3af5a4 Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Sat, 7 Jul 2018 09:16:50 +1000 Subject: [PATCH] Fix crashes when using an empty index --- include/kdbush.hpp | 4 ++++ test.cpp | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/include/kdbush.hpp b/include/kdbush.hpp index d343b2b..be68ec5 100644 --- a/include/kdbush.hpp +++ b/include/kdbush.hpp @@ -94,6 +94,8 @@ class KDBush { const TIndex right, const std::uint8_t axis) { + if (points.empty()) return; + if (right - left <= nodeSize) { for (auto i = left; i <= right; i++) { const TNumber x = std::get<0>(points[i]); @@ -125,6 +127,8 @@ class KDBush { const TIndex right, const std::uint8_t axis) { + if (points.empty()) return; + const TNumber r2 = r * r; if (right - left <= nodeSize) { diff --git a/test.cpp b/test.cpp index f31a1ab..b9d6e54 100644 --- a/test.cpp +++ b/test.cpp @@ -45,6 +45,12 @@ static void testRadius() { static void testEmpty() { auto emptyPoints = std::vector{}; kdbush::KDBush index(emptyPoints); + // test no crash when using empty index + TIds result; + index.within(50, 50, 20, [&result](const auto id) { result.push_back(id); }); + assert(result.empty()); + index.range(20, 30, 50, 70, [&result](const auto id) { result.push_back(id); }); + assert(result.empty()); } int main() {