From 67cd3f400d2dec7f5356c55d05aee20942bca0bc Mon Sep 17 00:00:00 2001 From: Lee Rhodes Date: Sun, 22 Nov 2026 12:07:00 -0800 Subject: [PATCH] HLL: reject HLL_8 register values above 63 on deserialize An HLL_8 register holds a value from 0 to 63, but both deserializers copied the register bytes as they were, so a corrupt image could carry a larger value into estimates and unions. Reject such images, as Go, Rust and Java do. Sketches written by this library are unchanged. Co-Authored-By: Claude Opus 5.5 --- hll/include/HllArray-internal.hpp | 14 +++++++++++++- hll/include/HllArray.hpp | 3 +++ hll/test/HllArrayTest.cpp | 23 +++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/hll/include/HllArray-internal.hpp b/hll/include/HllArray-internal.hpp index 1da49caba..818632378 100644 --- a/hll/include/HllArray-internal.hpp +++ b/hll/include/HllArray-internal.hpp @@ -118,6 +118,7 @@ HllArray* HllArray::newHll(const void* bytes, size_t len, const A& allocat if (len < static_cast(hll_constants::HLL_BYTE_ARR_START + arrayBytes)) { throw std::out_of_range("Input array too small to hold sketch image"); } + if (tgtHllType == HLL_8) { checkHll8Registers(data + hll_constants::HLL_BYTE_ARR_START, arrayBytes); } double hip, kxq0, kxq1; std::memcpy(&hip, data + hll_constants::HIP_ACCUM_DOUBLE, sizeof(double)); @@ -202,7 +203,8 @@ HllArray* HllArray::newHll(std::istream& is, const A& allocator) { 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 uint8_t auxLgIntArrSize = listHeader[4]; AuxHashMap* auxHashMap = AuxHashMap::deserialize(is, lgK, auxCount, auxLgIntArrSize, comapctFlag, allocator); @@ -507,6 +509,16 @@ uint32_t HllArray::hll6ArrBytes(uint8_t lgConfigK) { return ((numSlots * 3) >> 2) + 1; } +template +void HllArray::checkHll8Registers(const uint8_t* registers, uint32_t numRegisters) { + for (uint32_t i = 0; i < numRegisters; ++i) { + if (registers[i] > hll_constants::VAL_MASK_6) { + throw std::invalid_argument("Possible corruption: HLL_8 register " + std::to_string(i) + " has value " + + std::to_string(registers[i]) + ", above " + std::to_string(hll_constants::VAL_MASK_6)); + } + } +} + template uint32_t HllArray::hll8ArrBytes(uint8_t lgConfigK) { return 1 << lgConfigK; diff --git a/hll/include/HllArray.hpp b/hll/include/HllArray.hpp index 87994cb66..beb9715cb 100644 --- a/hll/include/HllArray.hpp +++ b/hll/include/HllArray.hpp @@ -87,6 +87,9 @@ class HllArray : public HllSketchImpl { static uint32_t hll6ArrBytes(uint8_t lgConfigK); static uint32_t hll8ArrBytes(uint8_t lgConfigK); + // an HLL_8 register holds a value from 0 to 63 + static void checkHll8Registers(const uint8_t* registers, uint32_t numRegisters); + virtual AuxHashMap* getAuxHashMap() const; void setRebuildKxqCurminFlag(bool rebuild); diff --git a/hll/test/HllArrayTest.cpp b/hll/test/HllArrayTest.cpp index 7314b672b..61e2960ed 100644 --- a/hll/test/HllArrayTest.cpp +++ b/hll/test/HllArrayTest.cpp @@ -138,6 +138,29 @@ TEST_CASE("hll array: check corrupt bytearray", "[hll_array]") { REQUIRE_THROWS_AS(hll_sketch::deserialize(bytes, 3), std::out_of_range); } +TEST_CASE("hll array: rejects an HLL_8 register above 63", "[hll_array]") { + hll_sketch sk(10, HLL_8); + for (int i = 0; i < 10000; ++i) sk.update(i); + REQUIRE(sk.get_estimate() > 0); + for (bool compact: {true, false}) { + CAPTURE(compact); + auto bytes = compact ? sk.serialize_compact() : sk.serialize_updatable(); + REQUIRE(hll_sketch::deserialize(bytes.data(), bytes.size()).get_estimate() == sk.get_estimate()); + // the registers are at the end of an HLL_8 image + for (uint8_t value: {64, 200, 255}) { + CAPTURE(value); + bytes.back() = value; + REQUIRE_THROWS_AS(hll_sketch::deserialize(bytes.data(), bytes.size()), std::invalid_argument); + std::stringstream ss(std::ios::in | std::ios::out | std::ios::binary); + ss.write(reinterpret_cast(bytes.data()), bytes.size()); + REQUIRE_THROWS_AS(hll_sketch::deserialize(ss), std::invalid_argument); + } + // 63 is the largest valid value + bytes.back() = 63; + REQUIRE_NOTHROW(hll_sketch::deserialize(bytes.data(), bytes.size())); + } +} + TEST_CASE("hll array: check corrupt stream", "[hll_array]") { uint8_t lgK = 6; hll_sketch sk1(lgK);