From 9f45adba28ed8cd822250f5717a80c806695cb08 Mon Sep 17 00:00:00 2001 From: Lee Rhodes Date: Sun, 22 Nov 2026 12:22:18 -0800 Subject: [PATCH] Tuple: check empty serial version 3 images strictly on deserialize An empty compact tuple image was accepted whatever its other header bits held. Both deserializers now require the 8-byte form every current writer produces, as theta does: preamble longs 1, serial version 3, family 9, sketch type 1, unused byte 0, flags read-only, empty and compact with the reserved bits clear. The ordered flag and the seed hash are ignored. Serial version 1 images are unchanged. The mask and test values match Java CompactTupleSketch. Co-Authored-By: Claude Opus 5.5 --- tuple/include/tuple_sketch.hpp | 12 ++++++ tuple/include/tuple_sketch_impl.hpp | 23 ++++++++++- tuple/test/tuple_sketch_test.cpp | 63 +++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) diff --git a/tuple/include/tuple_sketch.hpp b/tuple/include/tuple_sketch.hpp index 7b636a785..5a9ba3474 100644 --- a/tuple/include/tuple_sketch.hpp +++ b/tuple/include/tuple_sketch.hpp @@ -644,6 +644,18 @@ class compact_tuple_sketch: public tuple_sketch { virtual void print_specifics(std::ostringstream& os) const; + // The MASK selects which bits of the first 8 bytes of an empty serial version 3 image are examined. + // The TEST gives the required values of the examined bits; it must lie within the MASK. + // Examined: preamble longs 1, serial version 3, family 9, sketch type 1, byte 4 = 0 (unused); + // flags read-only, empty, compact set; big-endian and reserved bits 5, 6, 7 clear. + // The ordered flag is ignored: older Java versions wrote empty images without it (flags 0x0e). + // The seed hash is ignored: it may be 0 or the seed hash, depending on the writer. + // These must stay the same as in Java CompactTupleSketch. + static const uint64_t EMPTY_SKETCH_MASK = 0x0000EFFFFFFFFFFFULL; + static const uint64_t EMPTY_SKETCH_TEST = 0x00000E0001090301ULL; + static_assert((EMPTY_SKETCH_TEST & ~EMPTY_SKETCH_MASK) == 0, "EMPTY_SKETCH_TEST must lie within EMPTY_SKETCH_MASK"); + static void check_empty_v3(uint64_t pre0); + template friend class theta_union_base; template friend class theta_intersection_base; template friend class theta_set_difference_base; diff --git a/tuple/include/tuple_sketch_impl.hpp b/tuple/include/tuple_sketch_impl.hpp index 025079fb3..2d145e0a7 100644 --- a/tuple/include/tuple_sketch_impl.hpp +++ b/tuple/include/tuple_sketch_impl.hpp @@ -482,7 +482,7 @@ compact_tuple_sketch compact_tuple_sketch::deserialize(std::istream& const auto serial_version = read(is); const auto family = read(is); const auto type = read(is); - read(is); // unused + const auto unused = read(is); const auto flags_byte = read(is); const auto seed_hash = read(is); if (serial_version != SERIAL_VERSION && serial_version != SERIAL_VERSION_LEGACY) { @@ -496,6 +496,12 @@ compact_tuple_sketch compact_tuple_sketch::deserialize(std::istream& } const bool is_empty = flags_byte & (1 << flags::IS_EMPTY); if (!is_empty) checker::check_seed_hash(seed_hash, compute_seed_hash(seed)); + if (is_empty && serial_version == SERIAL_VERSION) { + check_empty_v3(static_cast(preamble_longs) | static_cast(serial_version) << 8 + | static_cast(family) << 16 | static_cast(type) << 24 + | static_cast(unused) << 32 | static_cast(flags_byte) << 40 + | static_cast(seed_hash) << 48); + } uint64_t theta = theta_constants::MAX_THETA; uint32_t num_entries = 0; @@ -557,6 +563,11 @@ compact_tuple_sketch compact_tuple_sketch::deserialize(const void* b } const bool is_empty = flags_byte & (1 << flags::IS_EMPTY); if (!is_empty) checker::check_seed_hash(seed_hash, compute_seed_hash(seed)); + if (is_empty && serial_version == SERIAL_VERSION) { + uint64_t pre0; + copy_from_mem(base, pre0); + check_empty_v3(pre0); + } uint64_t theta = theta_constants::MAX_THETA; uint32_t num_entries = 0; @@ -594,6 +605,16 @@ compact_tuple_sketch compact_tuple_sketch::deserialize(const void* b return compact_tuple_sketch(is_empty, is_ordered, seed_hash, theta, std::move(entries)); } +template +void compact_tuple_sketch::check_empty_v3(uint64_t pre0) { + const uint64_t masked = pre0 & EMPTY_SKETCH_MASK; + if (masked != EMPTY_SKETCH_TEST) { + std::stringstream s; + s << "empty compact tuple sketch preamble does not match: 0x" << std::hex << masked; + throw std::invalid_argument(s.str()); + } +} + template auto compact_tuple_sketch::begin() -> iterator { return iterator(entries_.data(), static_cast(entries_.size()), 0); diff --git a/tuple/test/tuple_sketch_test.cpp b/tuple/test/tuple_sketch_test.cpp index 15a85afe2..7b35ae619 100644 --- a/tuple/test/tuple_sketch_test.cpp +++ b/tuple/test/tuple_sketch_test.cpp @@ -132,6 +132,69 @@ TEST_CASE("tuple sketch float: empty", "[tuple_sketch]") { REQUIRE(update_sketch.compact(false).is_ordered()); } +TEST_CASE("tuple sketch: deserialize checks empty images strictly", "[tuple_sketch]") { + using sketch_type = compact_tuple_sketch; + using vector_bytes = sketch_type::vector_bytes; + const size_t serial_version_byte = 1; + const size_t type_byte = 3; + const size_t unused_byte = 4; + const size_t flags_byte = 5; + const size_t seed_hash_offset = 6; + const vector_bytes empty = update_tuple_sketch::builder().build().compact().serialize(); + REQUIRE(empty.size() == 8); + REQUIRE(empty[flags_byte] == 0x1e); + + auto deserialize_both = [](const vector_bytes& bytes) { + std::stringstream s(std::ios::in | std::ios::out | std::ios::binary); + s.write(reinterpret_cast(bytes.data()), bytes.size()); + REQUIRE(sketch_type::deserialize(s).is_empty()); + REQUIRE(sketch_type::deserialize(bytes.data(), bytes.size()).is_empty()); + }; + auto check_rejected = [](const vector_bytes& bytes) { + REQUIRE_THROWS_AS(sketch_type::deserialize(bytes.data(), bytes.size()), std::invalid_argument); + std::stringstream s(std::ios::in | std::ios::out | std::ios::binary); + s.write(reinterpret_cast(bytes.data()), bytes.size()); + REQUIRE_THROWS_AS(sketch_type::deserialize(s), std::invalid_argument); + }; + + SECTION("accepted") { + deserialize_both(empty); + // without the ordered flag, as older Java versions wrote + auto bytes = empty; + bytes[flags_byte] = 0x0e; + deserialize_both(bytes); + // any seed hash + bytes = empty; + bytes[seed_hash_offset] = 0xcc; + bytes[seed_hash_offset + 1] = 0x93; + deserialize_both(bytes); + // serial version 1 images are not checked + bytes = empty; + bytes[serial_version_byte] = 1; + bytes[unused_byte] = 1; + deserialize_both(bytes); + } + + SECTION("rejected") { + auto bytes = empty; + bytes[0] = 2; // preamble longs + check_rejected(bytes); + bytes = empty; + bytes[type_byte] = 5; // legacy sketch type + check_rejected(bytes); + bytes = empty; + bytes[unused_byte] = 1; + check_rejected(bytes); + // read-only or compact flag missing, big-endian or a reserved flag set + for (uint8_t flags: {0x1c, 0x16, 0x1f, 0x3e, 0x5e, 0x9e}) { + CAPTURE(static_cast(flags)); + bytes = empty; + bytes[flags_byte] = flags; + check_rejected(bytes); + } + } +} + TEST_CASE("tuple sketch: single item", "[tuple_sketch]") { auto update_sketch = update_tuple_sketch::builder().build(); update_sketch.update(1, 1.0f);