diff --git a/hll/include/HllArray-internal.hpp b/hll/include/HllArray-internal.hpp index 1da49cab..81863237 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 87994cb6..beb9715c 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 7314b672..61e2960e 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);