From 85e3096a5cac9995ed4f78289f7c9394a3f83119 Mon Sep 17 00:00:00 2001 From: Lee Rhodes Date: Mon, 30 Nov 2026 17:28:13 -0800 Subject: [PATCH 1/3] Bloom filter: check the header of a non-empty image on deserialize A non-empty image must have the standard 4-long preamble, since the number of bits set follows the empty preamble, and at least one hash function. The constructor used for a non-empty image does not check the number of hashes, unlike the public ones. The existing check for an empty bit array moves into the same helper, used by the bytes and stream paths. Co-Authored-By: Claude Opus 5.5 --- filters/include/bloom_filter.hpp | 3 +++ filters/include/bloom_filter_impl.hpp | 30 ++++++++++++++-------- filters/test/bloom_filter_test.cpp | 36 +++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/filters/include/bloom_filter.hpp b/filters/include/bloom_filter.hpp index 34fd9642..d5f1ce91 100644 --- a/filters/include/bloom_filter.hpp +++ b/filters/include/bloom_filter.hpp @@ -602,6 +602,9 @@ class bloom_filter_alloc { uint8_t* memory, const A& allocator); + // checks the header of a non-empty image before its bit array is read + static void check_non_empty_header(uint8_t prelongs, uint16_t num_hashes, uint64_t num_bits); + static bloom_filter_alloc internal_deserialize_or_wrap(void* bytes, size_t length_bytes, bool read_only, diff --git a/filters/include/bloom_filter_impl.hpp b/filters/include/bloom_filter_impl.hpp index 63ed0b95..0a8c8614 100644 --- a/filters/include/bloom_filter_impl.hpp +++ b/filters/include/bloom_filter_impl.hpp @@ -306,11 +306,7 @@ bloom_filter_alloc bloom_filter_alloc::deserialize(std::istream& is, const if (is_empty) { return bloom_filter_alloc(num_bits, num_hashes, seed, allocator); } - // a non-empty filter has a bit array: this keeps capacity_bits_ positive, - // and keeps allocate() from being called with 0 - if (num_bytes == 0) { - throw std::invalid_argument("Possible corruption: non-empty filter with an empty bit array"); - } + check_non_empty_header(prelongs, num_hashes, num_bits); const uint64_t raw_num_bits_set = read(is); @@ -344,6 +340,24 @@ bloom_filter_alloc bloom_filter_alloc::writable_wrap(void* bytes, size_t l return internal_deserialize_or_wrap(bytes, length_bytes, false, true, allocator); } +template +void bloom_filter_alloc::check_non_empty_header(uint8_t prelongs, uint16_t num_hashes, uint64_t num_bits) { + // the number of bits set follows the empty preamble, so a shorter preamble would read past it + if (prelongs != PREAMBLE_LONGS_STANDARD) { + throw std::invalid_argument("Possible corruption: non-empty filter with " + std::to_string(prelongs) + + " preamble longs, expected " + std::to_string(PREAMBLE_LONGS_STANDARD)); + } + // the public constructors check these, but the one used for a non-empty image does not + if (num_hashes == 0) { + throw std::invalid_argument("Possible corruption: non-empty filter with no hash functions"); + } + // a non-empty filter has a bit array: this keeps capacity_bits_ positive, + // and keeps allocate() from being called with 0 + if (num_bits == 0) { + throw std::invalid_argument("Possible corruption: non-empty filter with an empty bit array"); + } +} + template bloom_filter_alloc bloom_filter_alloc::internal_deserialize_or_wrap(void* bytes, size_t length_bytes, @@ -396,11 +410,7 @@ bloom_filter_alloc bloom_filter_alloc::internal_deserialize_or_wrap(void* } else if (is_empty) { return bloom_filter_alloc(num_bits, num_hashes, seed, allocator); } - // a non-empty filter has a bit array: this keeps capacity_bits_ positive, - // and keeps allocate() from being called with 0 - if (num_bytes == 0) { - throw std::invalid_argument("Possible corruption: non-empty filter with an empty bit array"); - } + check_non_empty_header(prelongs, num_hashes, num_bits); uint64_t raw_num_bits_set; ptr += copy_from_mem(ptr, raw_num_bits_set); diff --git a/filters/test/bloom_filter_test.cpp b/filters/test/bloom_filter_test.cpp index 51cdf433..d10e7600 100644 --- a/filters/test/bloom_filter_test.cpp +++ b/filters/test/bloom_filter_test.cpp @@ -474,6 +474,42 @@ TEST_CASE("bloom_filter: image with zero bit array length is rejected", "[bloom_ REQUIRE_THROWS_AS(bloom_filter::deserialize(empty_is), std::invalid_argument); } +TEST_CASE("bloom_filter: non-empty image header is checked", "[bloom_filter]") { + const size_t prelongs_offset = 0; + const size_t num_hashes_offset = 4; + + auto bf = bloom_filter::builder::create_by_size(64, 3); + bf.update("apple"); + const auto good = bf.serialize(); + + auto check_rejected = [](std::vector bytes) { + REQUIRE_THROWS_AS(bloom_filter::deserialize(bytes.data(), bytes.size()), std::invalid_argument); + REQUIRE_THROWS_AS(bloom_filter::wrap(bytes.data(), bytes.size()), std::invalid_argument); + REQUIRE_THROWS_AS(bloom_filter::writable_wrap(bytes.data(), bytes.size()), std::invalid_argument); + const std::string serialized(reinterpret_cast(bytes.data()), bytes.size()); + std::istringstream is(serialized, std::ios::in | std::ios::binary); + REQUIRE_THROWS_AS(bloom_filter::deserialize(is), std::invalid_argument); + }; + + SECTION("empty preamble length") { + // only the empty preamble's 24 bytes: the number of bits set would be read past the end + std::vector bytes(good.begin(), good.begin() + 24); + bytes[prelongs_offset] = 3; + check_rejected(bytes); + // with the whole image, the preamble length still has to match + bytes.assign(good.begin(), good.end()); + bytes[prelongs_offset] = 3; + check_rejected(bytes); + } + + SECTION("no hash functions") { + std::vector bytes(good.begin(), good.end()); + const uint16_t num_hashes = 0; + std::memcpy(bytes.data() + num_hashes_offset, &num_hashes, sizeof(num_hashes)); + check_rejected(bytes); + } +} + TEST_CASE("bloom_filter: capacity is rounded up to a multiple of 64 bits", "[bloom_filter]") { // so the bit array of a filter is never shorter than 8 bytes REQUIRE(bloom_filter::builder::create_by_size(1, 3).get_capacity() == 64); From 5afea45d0b15f2a3972322991fd441fe88586615 Mon Sep 17 00:00:00 2001 From: Lee Rhodes Date: Tue, 1 Dec 2026 16:46:55 -0800 Subject: [PATCH 2/3] Check header fields on deserialize in HLL, density, tuple and VarOpt - HLL: an image whose target type is not HLL_4 must not declare aux entries; only HLL_4 has an aux map. Checked in both readers before the aux map is built. - Density: each level size is read only when the input has room for it, and must not exceed the retained items left to read. The stream reader stops at the end of the stream. The dimension must be at least 1, in the constructor and in both readers, so that every sketch that can be built can also be read back. - Compact tuple: a non-empty image must have 1, 2 or 3 preamble longs, and the bytes reader checks that all of them are present before reading them. - VarOpt: the full-mode check h + r == k is done in 64 bits, and h must fit the arrays the reader allocates. Co-Authored-By: Claude Opus 5.5 --- density/include/density_sketch.hpp | 4 +- density/include/density_sketch_impl.hpp | 21 ++++++++++ density/test/density_sketch_test.cpp | 52 ++++++++++++++++++++++++ hll/include/HllArray-internal.hpp | 14 ++++++- hll/include/HllArray.hpp | 1 + hll/test/HllArrayTest.cpp | 21 ++++++++++ sampling/include/var_opt_sketch_impl.hpp | 12 ++++-- sampling/test/var_opt_sketch_test.cpp | 24 +++++++++++ tuple/include/tuple_sketch_impl.hpp | 17 +++++--- tuple/test/tuple_sketch_test.cpp | 35 ++++++++++++++++ 10 files changed, 189 insertions(+), 12 deletions(-) diff --git a/density/include/density_sketch.hpp b/density/include/density_sketch.hpp index 8a064b1a..95d8eb5c 100755 --- a/density/include/density_sketch.hpp +++ b/density/include/density_sketch.hpp @@ -67,7 +67,7 @@ class density_sketch { /** * Constructor * @param k controls the size and error of the sketch. - * @param dim dimension of the input domain + * @param dim dimension of the input domain, at least 1 * @param kernel to use by this instance * @param allocator to use by this instance */ @@ -220,6 +220,8 @@ class density_sketch { static void check_serial_version(uint8_t serial_version); static void check_family_id(uint8_t family_id, uint8_t serial_version); static void check_header_validity(uint8_t preamble_ints, uint8_t flags_byte, uint8_t serial_version); + static void check_dim(uint32_t dim); + static void check_level_size(uint32_t level_size, int64_t num_to_read); density_sketch(uint16_t k, uint32_t dim, uint32_t num_retained, uint64_t n, Levels&& levels, const Kernel& kernel = Kernel()); diff --git a/density/include/density_sketch_impl.hpp b/density/include/density_sketch_impl.hpp index 1e9113d1..95ba79a3 100755 --- a/density/include/density_sketch_impl.hpp +++ b/density/include/density_sketch_impl.hpp @@ -38,6 +38,7 @@ n_(0), levels_(1, Level(allocator), allocator) { check_k(k); + check_dim(dim); } template @@ -51,6 +52,7 @@ n_(n), levels_(std::move(levels)) { check_k(k); + check_dim(dim); } template @@ -283,6 +285,7 @@ density_sketch density_sketch::deserialize(std::istream& is, c return density_sketch(k, dim, kernel, allocator); } + check_dim(dim); const auto num_retained = read(is); const auto n = read(is); @@ -292,6 +295,8 @@ density_sketch density_sketch::deserialize(std::istream& is, c int64_t num_to_read = num_retained; // num_retrained is uint32_t so this allows error checking while (num_to_read > 0) { const auto level_size = read(is); + if (!is.good()) throw std::runtime_error("error reading from std::istream"); + check_level_size(level_size, num_to_read); Level lvl(allocator); lvl.reserve(level_size); for (uint32_t i = 0; i < level_size; ++i) { @@ -340,6 +345,7 @@ density_sketch density_sketch::deserialize(const void* bytes, return density_sketch(k, dim, kernel, allocator); } + check_dim(dim); ensure_minimum_memory(size, PREAMBLE_INTS_LONG * sizeof(uint32_t)); uint32_t num_retained; ptr += copy_from_mem(ptr, num_retained); @@ -356,8 +362,10 @@ density_sketch density_sketch::deserialize(const void* bytes, Levels levels(allocator); int64_t num_to_read = num_retained; // num_retained is uint32_t so this allows error checking while (num_to_read > 0) { + ensure_minimum_memory(end_ptr - ptr, sizeof(uint32_t)); uint32_t level_size; ptr += copy_from_mem(ptr, level_size); + check_level_size(level_size, num_to_read); ensure_minimum_memory(end_ptr - ptr, level_size * pt_size); Level lvl(allocator); lvl.reserve(level_size); @@ -377,6 +385,19 @@ density_sketch density_sketch::deserialize(const void* bytes, return density_sketch(k, dim, num_retained, n, std::move(levels), kernel); } +template +void density_sketch::check_dim(uint32_t dim) { + if (dim == 0) throw std::invalid_argument("dimension must be at least 1"); +} + +template +void density_sketch::check_level_size(uint32_t level_size, int64_t num_to_read) { + if (level_size > num_to_read) { + throw std::invalid_argument("Possible corruption: level size " + std::to_string(level_size) + + " exceeds the " + std::to_string(num_to_read) + " retained items left to read"); + } +} + template void density_sketch::check_k(uint16_t k) { if (k < 2) diff --git a/density/test/density_sketch_test.cpp b/density/test/density_sketch_test.cpp index 9837b65e..ee1303e7 100755 --- a/density/test/density_sketch_test.cpp +++ b/density/test/density_sketch_test.cpp @@ -17,7 +17,10 @@ * under the License. */ +#include #include +#include +#include #include #include @@ -498,4 +501,53 @@ TEST_CASE("density sketch: serialize with a header", "[density_sketch]") { } } + +TEST_CASE("density sketch: level sizes and dimension are checked on deserialize", "[density_sketch]") { + density_sketch sk(10, 3); + sk.update(std::vector{1, 2, 3}); + const auto valid = sk.serialize(); + const size_t header_size = 24; // 6 preamble ints + const size_t dim_offset = 8; + REQUIRE(valid.size() == header_size + sizeof(uint32_t) + 3 * sizeof(float)); + + auto check = [](const std::vector& bytes) { + REQUIRE_THROWS(density_sketch::deserialize(bytes.data(), bytes.size())); + std::stringstream ss; + ss.write(reinterpret_cast(bytes.data()), bytes.size()); + REQUIRE_THROWS(density_sketch::deserialize(ss)); + }; + + SECTION("empty levels to the end of the input") { + auto bytes = valid; + std::fill(bytes.begin() + header_size, bytes.end(), 0); + check(bytes); + } + + SECTION("level larger than the retained items") { + auto bytes = valid; + const uint32_t level_size = 2; + std::memcpy(bytes.data() + header_size, &level_size, sizeof(level_size)); + check(bytes); + } + + SECTION("dimension 0") { + auto bytes = valid; + const uint32_t dim = 0; + std::memcpy(bytes.data() + dim_offset, &dim, sizeof(dim)); + check(bytes); + } + + SECTION("dimension 0, empty") { + density_sketch empty(10, 3); + auto bytes = empty.serialize(); + const uint32_t dim = 0; + std::memcpy(bytes.data() + dim_offset, &dim, sizeof(dim)); + check(bytes); + } +} + +TEST_CASE("density sketch: dimension must be at least 1", "[density_sketch]") { + REQUIRE_THROWS_AS(density_sketch(10, 0), std::invalid_argument); +} + } /* namespace datasketches */ diff --git a/hll/include/HllArray-internal.hpp b/hll/include/HllArray-internal.hpp index c626e94a..187f89a2 100644 --- a/hll/include/HllArray-internal.hpp +++ b/hll/include/HllArray-internal.hpp @@ -128,11 +128,12 @@ HllArray* HllArray::newHll(const void* bytes, size_t len, const A& allocat uint32_t numAtCurMin, auxCount; std::memcpy(&numAtCurMin, data + hll_constants::CUR_MIN_COUNT_INT, sizeof(int)); std::memcpy(&auxCount, data + hll_constants::AUX_COUNT_INT, sizeof(int)); + checkAuxCount(auxCount, tgtHllType); AuxHashMap* auxHashMap = nullptr; typedef std::unique_ptr, std::function*)>> aux_hash_map_ptr; aux_hash_map_ptr aux_ptr; - if (auxCount > 0) { // necessarily TgtHllType == HLL_4 + if (auxCount > 0) { // TgtHllType == HLL_4, see checkAuxCount() uint8_t auxLgIntArrSize = data[4]; const size_t offset = hll_constants::HLL_BYTE_ARR_START + arrayBytes; const uint8_t* auxDataStart = data + offset; @@ -200,12 +201,13 @@ HllArray* HllArray::newHll(std::istream& is, const A& allocator) { const auto numAtCurMin = read(is); const auto auxCount = read(is); + checkAuxCount(auxCount, tgtHllType); sketch->putNumAtCurMin(numAtCurMin); read(is, sketch->hllByteArr_.data(), sketch->getHllByteArrBytes()); if (tgtHllType == HLL_8) { checkHll8Registers(sketch->hllByteArr_.data(), sketch->getHllByteArrBytes()); } - if (auxCount > 0) { // necessarily TgtHllType == HLL_4 + if (auxCount > 0) { // TgtHllType == HLL_4, see checkAuxCount() uint8_t auxLgIntArrSize = listHeader[4]; AuxHashMap* auxHashMap = AuxHashMap::deserialize(is, lgK, auxCount, auxLgIntArrSize, comapctFlag, allocator); ((Hll4Array*)sketch)->putAuxHashMap(auxHashMap); @@ -216,6 +218,14 @@ HllArray* HllArray::newHll(std::istream& is, const A& allocator) { return sketch_ptr.release(); } +template +void HllArray::checkAuxCount(uint32_t auxCount, target_hll_type tgtHllType) { + // only HLL_4 has an aux map + if (auxCount > 0 && tgtHllType != HLL_4) { + throw std::invalid_argument("Possible corruption: aux entries in a non-HLL_4 image"); + } +} + template auto HllArray::serialize(bool compact, unsigned header_size_bytes) const -> vector_bytes { const size_t sketchSizeBytes = (compact ? getCompactSerializationBytes() : getUpdatableSerializationBytes()) + header_size_bytes; diff --git a/hll/include/HllArray.hpp b/hll/include/HllArray.hpp index beb9715c..d72e0e74 100644 --- a/hll/include/HllArray.hpp +++ b/hll/include/HllArray.hpp @@ -89,6 +89,7 @@ class HllArray : public HllSketchImpl { // an HLL_8 register holds a value from 0 to 63 static void checkHll8Registers(const uint8_t* registers, uint32_t numRegisters); + static void checkAuxCount(uint32_t auxCount, target_hll_type tgtHllType); virtual AuxHashMap* getAuxHashMap() const; diff --git a/hll/test/HllArrayTest.cpp b/hll/test/HllArrayTest.cpp index 61e2960e..3067ef34 100644 --- a/hll/test/HllArrayTest.cpp +++ b/hll/test/HllArrayTest.cpp @@ -19,6 +19,7 @@ #include "hll.hpp" +#include #include #include #include @@ -212,4 +213,24 @@ TEST_CASE("hll array: check corrupt stream", "[hll_array]") { ss.put((char)tmp); } + +TEST_CASE("hll array: aux count is checked against the target type", "[hll_array]") { + for (const target_hll_type type : {HLL_6, HLL_8}) { + hll_sketch sk(8, type); + for (int i = 0; i < 1000; ++i) sk.update(i); + auto bytes = sk.serialize_compact(); + const uint32_t aux_count = 1; + std::memcpy(bytes.data() + hll_constants::AUX_COUNT_INT, &aux_count, sizeof(aux_count)); + // one compact aux entry after the registers: slot 1, value 20 + const uint32_t aux_entry = (20u << 26) | 1u; + const uint8_t* entry_bytes = reinterpret_cast(&aux_entry); + bytes.insert(bytes.end(), entry_bytes, entry_bytes + sizeof(aux_entry)); + + REQUIRE_THROWS_AS(hll_sketch::deserialize(bytes.data(), bytes.size()), std::invalid_argument); + std::stringstream ss; + ss.write(reinterpret_cast(bytes.data()), bytes.size()); + REQUIRE_THROWS_AS(hll_sketch::deserialize(ss), std::invalid_argument); + } +} + } /* namespace datasketches */ diff --git a/sampling/include/var_opt_sketch_impl.hpp b/sampling/include/var_opt_sketch_impl.hpp index b660d75b..07d577e8 100644 --- a/sampling/include/var_opt_sketch_impl.hpp +++ b/sampling/include/var_opt_sketch_impl.hpp @@ -1364,14 +1364,20 @@ uint32_t var_opt_sketch::validate_and_get_target_size(uint32_t preamble_lo throw std::invalid_argument("Possible corruption: deserializing with n > k but not in full mode. " "Found n = " + std::to_string(n) + ", k = " + std::to_string(k)); } - if (h + r != k) { - throw std::invalid_argument("Possible corruption: deserializing in full mode but h + r != n. " - "Found h = " + std::to_string(h) + ", r = " + std::to_string(r) + ", n = " + std::to_string(n)); + if (static_cast(h) + r != k) { + throw std::invalid_argument("Possible corruption: deserializing in full mode but h + r != k. " + "Found h = " + std::to_string(h) + ", r = " + std::to_string(r) + ", k = " + std::to_string(k)); } array_size = k + 1; } + // the readers copy h weights and items into arrays of this size + if (h > array_size) { + throw std::invalid_argument("Possible corruption: h = " + std::to_string(h) + + " exceeds the array size " + std::to_string(array_size)); + } + return array_size; } diff --git a/sampling/test/var_opt_sketch_test.cpp b/sampling/test/var_opt_sketch_test.cpp index 472700e9..92543ede 100644 --- a/sampling/test/var_opt_sketch_test.cpp +++ b/sampling/test/var_opt_sketch_test.cpp @@ -21,6 +21,7 @@ #include +#include #include #include #include @@ -576,4 +577,27 @@ TEST_CASE("varopt sketch: serialize with a header", "[var_opt_sketch]") { } } + +TEST_CASE("varopt sketch: h + r is checked without wrapping", "[var_opt_sketch]") { + var_opt_sketch sk(5); + for (int i = 0; i < 6; ++i) sk.update(i); + const auto valid = sk.serialize(); + // full mode: 4 preamble longs (32 bytes), h at offset 16, r at offset 20 + std::vector bytes(valid.begin(), valid.begin() + 32); + const uint32_t h = 7; + const uint32_t r = 0xFFFFFFFE; // h + r == k modulo 2^32 + std::memcpy(bytes.data() + 16, &h, sizeof(h)); + std::memcpy(bytes.data() + 20, &r, sizeof(r)); + const double weight = 1.0; + for (uint32_t i = 0; i < h; ++i) { + const uint8_t* w = reinterpret_cast(&weight); + bytes.insert(bytes.end(), w, w + sizeof(weight)); + } + + REQUIRE_THROWS_AS(var_opt_sketch::deserialize(bytes.data(), bytes.size()), std::invalid_argument); + std::stringstream ss; + ss.write(reinterpret_cast(bytes.data()), bytes.size()); + REQUIRE_THROWS_AS(var_opt_sketch::deserialize(ss), std::invalid_argument); +} + } diff --git a/tuple/include/tuple_sketch_impl.hpp b/tuple/include/tuple_sketch_impl.hpp index 2d145e0a..3e74fcea 100644 --- a/tuple/include/tuple_sketch_impl.hpp +++ b/tuple/include/tuple_sketch_impl.hpp @@ -508,12 +508,15 @@ compact_tuple_sketch compact_tuple_sketch::deserialize(std::istream& if (!is_empty) { if (preamble_longs == 1) { num_entries = 1; - } else { + } else if (preamble_longs == 2 || preamble_longs == 3) { num_entries = read(is); read(is); // unused - if (preamble_longs > 2) { + if (preamble_longs == 3) { theta = read(is); } + } else { + throw std::invalid_argument("Possible corruption: non-empty sketch with " + std::to_string(preamble_longs) + + " preamble longs, expected 1, 2 or 3"); } } A alloc(allocator); @@ -575,14 +578,16 @@ compact_tuple_sketch compact_tuple_sketch::deserialize(const void* b if (!is_empty) { if (preamble_longs == 1) { num_entries = 1; - } else { - ensure_minimum_memory(size, 8); // read the first prelong before this method + } else if (preamble_longs == 2 || preamble_longs == 3) { + ensure_minimum_memory(size, static_cast(preamble_longs) << 3); ptr += copy_from_mem(ptr, num_entries); ptr += sizeof(uint32_t); // unused - if (preamble_longs > 2) { - ensure_minimum_memory(size, (preamble_longs - 1) << 3); + if (preamble_longs == 3) { ptr += copy_from_mem(ptr, theta); } + } else { + throw std::invalid_argument("Possible corruption: non-empty sketch with " + std::to_string(preamble_longs) + + " preamble longs, expected 1, 2 or 3"); } } const size_t keys_size_bytes = sizeof(uint64_t) * num_entries; diff --git a/tuple/test/tuple_sketch_test.cpp b/tuple/test/tuple_sketch_test.cpp index 7b35ae61..d9e7082d 100644 --- a/tuple/test/tuple_sketch_test.cpp +++ b/tuple/test/tuple_sketch_test.cpp @@ -18,6 +18,7 @@ */ #include +#include #include #include #include @@ -511,4 +512,38 @@ TEST_CASE("tuple sketch: serialize with a header", "[tuple_sketch]") { } } + +TEST_CASE("tuple sketch: preamble length is checked on deserialize", "[tuple_sketch]") { + auto update_sketch = update_tuple_sketch::builder().build(); + update_sketch.update(1, 1.0); + update_sketch.update(2, 1.0); + const auto valid = update_sketch.compact().serialize(); + REQUIRE(valid[0] == 2); + + auto check = [](const std::vector& bytes) { + REQUIRE_THROWS(compact_tuple_sketch::deserialize(bytes.data(), bytes.size())); + std::stringstream ss; + ss.write(reinterpret_cast(bytes.data()), bytes.size()); + REQUIRE_THROWS(compact_tuple_sketch::deserialize(ss)); + }; + + SECTION("0 preamble longs, header only") { + std::vector bytes(valid.begin(), valid.begin() + 8); + bytes[0] = 0; + check(bytes); + } + + SECTION("3 preamble longs, two longs given") { + std::vector bytes(valid.begin(), valid.begin() + 16); + bytes[0] = 3; + check(bytes); + } + + SECTION("4 preamble longs") { + auto bytes = valid; + bytes[0] = 4; + check(bytes); + } +} + } /* namespace datasketches */ From 62c4072b96259a02c20937dd72595b40e91f4bc7 Mon Sep 17 00:00:00 2001 From: Lee Rhodes Date: Wed, 2 Dec 2026 12:42:12 -0800 Subject: [PATCH 3/3] Density: a non-empty image must retain at least one item Suggested by proost in review. Co-Authored-By: Claude Opus 5.5 --- density/include/density_sketch.hpp | 1 + density/include/density_sketch_impl.hpp | 9 +++++++++ density/test/density_sketch_test.cpp | 9 +++++++++ 3 files changed, 19 insertions(+) diff --git a/density/include/density_sketch.hpp b/density/include/density_sketch.hpp index 95d8eb5c..3edfba0e 100755 --- a/density/include/density_sketch.hpp +++ b/density/include/density_sketch.hpp @@ -221,6 +221,7 @@ class density_sketch { static void check_family_id(uint8_t family_id, uint8_t serial_version); static void check_header_validity(uint8_t preamble_ints, uint8_t flags_byte, uint8_t serial_version); static void check_dim(uint32_t dim); + static void check_num_retained(uint32_t num_retained); static void check_level_size(uint32_t level_size, int64_t num_to_read); density_sketch(uint16_t k, uint32_t dim, uint32_t num_retained, uint64_t n, Levels&& levels, diff --git a/density/include/density_sketch_impl.hpp b/density/include/density_sketch_impl.hpp index 95ba79a3..cbeea57a 100755 --- a/density/include/density_sketch_impl.hpp +++ b/density/include/density_sketch_impl.hpp @@ -287,6 +287,7 @@ density_sketch density_sketch::deserialize(std::istream& is, c check_dim(dim); const auto num_retained = read(is); + check_num_retained(num_retained); const auto n = read(is); // levels arrays @@ -349,6 +350,7 @@ density_sketch density_sketch::deserialize(const void* bytes, ensure_minimum_memory(size, PREAMBLE_INTS_LONG * sizeof(uint32_t)); uint32_t num_retained; ptr += copy_from_mem(ptr, num_retained); + check_num_retained(num_retained); uint64_t n; ptr += copy_from_mem(ptr, n); @@ -390,6 +392,13 @@ void density_sketch::check_dim(uint32_t dim) { if (dim == 0) throw std::invalid_argument("dimension must be at least 1"); } +template +void density_sketch::check_num_retained(uint32_t num_retained) { + if (num_retained == 0) { + throw std::invalid_argument("Possible corruption: a non-empty sketch must retain at least one item"); + } +} + template void density_sketch::check_level_size(uint32_t level_size, int64_t num_to_read) { if (level_size > num_to_read) { diff --git a/density/test/density_sketch_test.cpp b/density/test/density_sketch_test.cpp index ee1303e7..711a3202 100755 --- a/density/test/density_sketch_test.cpp +++ b/density/test/density_sketch_test.cpp @@ -530,6 +530,15 @@ TEST_CASE("density sketch: level sizes and dimension are checked on deserialize" check(bytes); } + SECTION("no retained items in a non-empty sketch") { + auto bytes = valid; + const size_t num_retained_offset = 12; + const uint32_t num_retained = 0; + std::memcpy(bytes.data() + num_retained_offset, &num_retained, sizeof(num_retained)); + bytes.resize(header_size); + check(bytes); + } + SECTION("dimension 0") { auto bytes = valid; const uint32_t dim = 0;