From de0d754693530a562bc9da2ecd1a7556ca7328e2 Mon Sep 17 00:00:00 2001 From: Symmetricity <184246+Symmetricity@users.noreply.github.com> Date: Thu, 28 May 2026 14:33:17 +0200 Subject: [PATCH] Fix UsedWays bounds check UsedWays stores relation-used way ids in a vector. The old exact-size check treated wayid == size() as valid and then indexed one past the vector. Resize when the requested id is equal to the current size, and treat the same boundary as absent when reading. Add coverage for the first and exact-size insert cases. Co-authored-by: Codex --- Makefile | 5 +++++ include/osm_store.h | 4 ++-- test/osm_store.test.cpp | 27 +++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 test/osm_store.test.cpp diff --git a/Makefile b/Makefile index 38908e07..b08088f2 100644 --- a/Makefile +++ b/Makefile @@ -149,6 +149,7 @@ test: \ test_significant_tags \ test_sorted_node_store \ test_sorted_way_store \ + test_osm_store \ test_tile_coordinates_set test_append_vector: \ @@ -188,6 +189,10 @@ test_options_parser: \ test/options_parser.test.o $(CXX) $(CXXFLAGS) -o test.options_parser $^ $(INC) $(LIB) $(LDFLAGS) && ./test.options_parser +test_osm_store: \ + test/osm_store.test.o + $(CXX) $(CXXFLAGS) -o test.osm_store $^ $(INC) $(LIB) $(LDFLAGS) && ./test.osm_store + test_pooled_string: \ src/mmap_allocator.o \ src/pooled_string.o \ diff --git a/include/osm_store.h b/include/osm_store.h index 171e9386..ce54cd0f 100644 --- a/include/osm_store.h +++ b/include/osm_store.h @@ -78,13 +78,13 @@ class UsedWays { // Mark a way as used void insert(WayID wayid) { std::lock_guard lock(mutex); - if (wayid>usedList.size()) usedList.resize(wayid+256); + if (wayid>=usedList.size()) usedList.resize(wayid+256); usedList[wayid] = true; } // See if a way is used bool at(WayID wayid) const { - return (wayid>usedList.size()) ? false : usedList[wayid]; + return (wayid>=usedList.size()) ? false : usedList[wayid]; } void clear() { diff --git a/test/osm_store.test.cpp b/test/osm_store.test.cpp new file mode 100644 index 00000000..61eca883 --- /dev/null +++ b/test/osm_store.test.cpp @@ -0,0 +1,27 @@ +#include +#include "external/minunit.h" +#include "osm_store.h" + +MU_TEST(test_usedways_grows_for_first_index) { + UsedWays usedWays; + usedWays.insert(0); + mu_check(usedWays.at(0)); +} + +MU_TEST(test_usedways_grows_at_current_size) { + UsedWays usedWays; + usedWays.insert(0); + usedWays.insert(256); + mu_check(usedWays.at(256)); +} + +MU_TEST_SUITE(test_suite_osm_store) { + MU_RUN_TEST(test_usedways_grows_for_first_index); + MU_RUN_TEST(test_usedways_grows_at_current_size); +} + +int main() { + MU_RUN_SUITE(test_suite_osm_store); + MU_REPORT(); + return MU_EXIT_CODE; +}