From eabcc545f59d7956b711f16f38cd776f3061ccf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Milo=C5=A1=20Stojko?= Date: Thu, 12 Nov 2026 10:12:39 +0000 Subject: [PATCH] fix: kll_sketch::update() copies the item before a compaction can free it If the item refers to an item of the same sketch and level 0 is full, internal_update() compacts first, which can destroy that item or free items_. update() then read freed memory. Adds a test for this compaction case, and proost's test from #569 for the buffer growth case, which passes on master. Co-authored-by: Isaac --- kll/include/kll_sketch_impl.hpp | 11 +++++-- kll/test/kll_sketch_test.cpp | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/kll/include/kll_sketch_impl.hpp b/kll/include/kll_sketch_impl.hpp index e1ed85ec..6e518b26 100644 --- a/kll/include/kll_sketch_impl.hpp +++ b/kll/include/kll_sketch_impl.hpp @@ -181,8 +181,15 @@ template void kll_sketch::update(FwdT&& item) { if (!check_update_item(item)) { return; } update_min_max(static_cast(item)); // min and max are always copies - const uint32_t index = internal_update(); - new (&items_[index]) T(std::forward(item)); + if (levels_[0] == 0) { + // item can refer into items_, and internal_update() can destroy it + T copy(std::forward(item)); + const uint32_t index = internal_update(); + new (&items_[index]) T(std::move(copy)); + } else { + const uint32_t index = internal_update(); + new (&items_[index]) T(std::forward(item)); + } reset_sorted_view(); } diff --git a/kll/test/kll_sketch_test.cpp b/kll/test/kll_sketch_test.cpp index 05eea991..490348a0 100644 --- a/kll/test/kll_sketch_test.cpp +++ b/kll/test/kll_sketch_test.cpp @@ -915,4 +915,55 @@ TEST_CASE("kll sketch: deserialize rejects inconsistent levels", "[kll_sketch]") } } +// Compaction: level 0 is full, so update(item) compacts it, which can free the item it reads +TEST_CASE("kll sketch: update with aliased item during compaction", "[kll_sketch]") { + const uint16_t k = 200; + kll_sketch sketch(k); + + for (int i = 0; i < kll_constants::DEFAULT_K; ++i) { + sketch.update(std::string(64, 'x') + std::to_string(i)); + } + + REQUIRE(sketch.get_n() == kll_constants::DEFAULT_K); + + const std::string& item = sketch.begin()->first; + const std::string expected = item; + + // Triggers compaction + sketch.update(item); + + REQUIRE(sketch.get_n() == kll_constants::DEFAULT_K + 1); + + // the compaction emptied level 0, so the first item is the one just added + REQUIRE(sketch.begin()->first == expected); + REQUIRE(sketch.begin()->second == 1); +} + +// Regrowth: if the buffer starts below k items, update(item) grows it, which can free the item +TEST_CASE("kll sketch: update with aliased item during buffer growth", "[kll_sketch]") { + const uint16_t k = 5000; + kll_sketch sketch(k); + + for (int i = 0; i < kll_constants::DEFAULT_K; ++i) { + sketch.update(std::string(64, 'x') + std::to_string(i)); + } + + REQUIRE(sketch.get_n() == kll_constants::DEFAULT_K); + + const std::string& item = sketch.begin()->first; + const std::string expected = item; + + sketch.update(item); + + REQUIRE(sketch.get_n() == kll_constants::DEFAULT_K + 1); + + uint32_t count = 0; + for (const auto& entry : sketch) { + if (entry.first == expected) { + ++count; + } + } + REQUIRE(count == 2); +} + } /* namespace datasketches */