From 5cf72f6c5b446dcf9ef19964f7c3bd14a7659b33 Mon Sep 17 00:00:00 2001 From: lani_karrot Date: Fri, 13 Feb 2026 13:15:30 +0900 Subject: [PATCH] fix: wrong k condition check --- sampling/include/var_opt_sketch_impl.hpp | 2 +- sampling/test/var_opt_sketch_test.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/sampling/include/var_opt_sketch_impl.hpp b/sampling/include/var_opt_sketch_impl.hpp index 36ee3fc8..dec5f2c2 100644 --- a/sampling/include/var_opt_sketch_impl.hpp +++ b/sampling/include/var_opt_sketch_impl.hpp @@ -145,7 +145,7 @@ var_opt_sketch::var_opt_sketch(var_opt_sketch&& other) noexcept : template var_opt_sketch::var_opt_sketch(uint32_t k, resize_factor rf, bool is_gadget, const A& allocator) : k_(k), h_(0), m_(0), r_(0), n_(0), total_wt_r_(0.0), rf_(rf), allocator_(allocator) { - if (k == 0 || k_ > MAX_K) { + if (k < 1 || k_ > MAX_K) { throw std::invalid_argument("k must be at least 1 and less than 2^31 - 1"); } diff --git a/sampling/test/var_opt_sketch_test.cpp b/sampling/test/var_opt_sketch_test.cpp index 71d16e91..6b84689f 100644 --- a/sampling/test/var_opt_sketch_test.cpp +++ b/sampling/test/var_opt_sketch_test.cpp @@ -71,6 +71,7 @@ static void check_if_equal(var_opt_sketch& sk1, var_opt_sketch& sk2) TEST_CASE("varopt sketch: invalid k", "[var_opt_sketch]") { REQUIRE_THROWS_AS(var_opt_sketch(0), std::invalid_argument); REQUIRE_THROWS_AS(var_opt_sketch(1U << 31), std::invalid_argument); // aka k < 0 + REQUIRE_THROWS_AS(var_opt_sketch(-1), std::invalid_argument); } TEST_CASE("varopt sketch: bad serialization version", "[var_opt_sketch]") {