Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion hll/include/HllArray-internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ HllArray<A>* HllArray<A>::newHll(const void* bytes, size_t len, const A& allocat
if (len < static_cast<size_t>(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));
Expand Down Expand Up @@ -202,7 +203,8 @@ HllArray<A>* HllArray<A>::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<A>* auxHashMap = AuxHashMap<A>::deserialize(is, lgK, auxCount, auxLgIntArrSize, comapctFlag, allocator);
Expand Down Expand Up @@ -507,6 +509,16 @@ uint32_t HllArray<A>::hll6ArrBytes(uint8_t lgConfigK) {
return ((numSlots * 3) >> 2) + 1;
}

template<typename A>
void HllArray<A>::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<typename A>
uint32_t HllArray<A>::hll8ArrBytes(uint8_t lgConfigK) {
return 1 << lgConfigK;
Expand Down
3 changes: 3 additions & 0 deletions hll/include/HllArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class HllArray : public HllSketchImpl<A> {
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<A>* getAuxHashMap() const;

void setRebuildKxqCurminFlag(bool rebuild);
Expand Down
23 changes: 23 additions & 0 deletions hll/test/HllArrayTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*>(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);
Expand Down
Loading