From 7ea69442b3c877a58340a833ebfd4ac88010c581 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Wed, 3 May 2023 12:58:29 +0800 Subject: [PATCH] c++: fix off-by-one error causing out-of-bounds access --- cpp/pmtiles.hpp | 2 +- cpp/test.cpp | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/cpp/pmtiles.hpp b/cpp/pmtiles.hpp index 9fe00c7..bf5e382 100644 --- a/cpp/pmtiles.hpp +++ b/cpp/pmtiles.hpp @@ -484,7 +484,7 @@ inline std::tuple build_root_leaves(const std::fu std::vector root_entries; std::string leaves_bytes; int num_leaves = 0; - for (size_t i = 0; i <= entries.size(); i += leaf_size) { + for (size_t i = 0; i < entries.size(); i += leaf_size) { num_leaves++; int end = i + leaf_size; if (i + leaf_size > entries.size()) { diff --git a/cpp/test.cpp b/cpp/test.cpp index 37afb5e..3f83237 100644 --- a/cpp/test.cpp +++ b/cpp/test.cpp @@ -196,6 +196,16 @@ string mydecompress(const string &input, uint8_t compression) { return input; } +MU_TEST(test_build_dirs_one) { + vector entries; + entries.push_back(entryv3(0, 0, 1, 2)); + string root_bytes; + string leaves_bytes; + int num_leaves; + tie(root_bytes, leaves_bytes, num_leaves) = build_root_leaves(&mycompress, pmtiles::COMPRESSION_NONE, entries, 1); + mu_check(num_leaves == 1); +} + MU_TEST(test_build_dirs) { vector entries; for (int i = 0; i < 100000; i += 2) { @@ -259,6 +269,7 @@ MU_TEST_SUITE(test_suite) { MU_RUN_TEST(test_serialize_header); MU_RUN_TEST(test_deserialize_header); MU_RUN_TEST(test_build_dirs); + MU_RUN_TEST(test_build_dirs_one); } int main(int argc, char *argv[]) {