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
2 changes: 1 addition & 1 deletion req/include/req_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ void req_sketch<T, C, A>::check_k(uint16_t k) {
template<typename T, typename C, typename A>
void req_sketch<T, C, A>::check_num_levels(uint8_t num_levels, bool raw_items) {
// a non-empty sketch has at least one compactor, and raw items fit in one
if (num_levels == 0 || (raw_items && num_levels != 1)) {
if (num_levels == 0 || num_levels > 64 || (raw_items && num_levels != 1)) {
throw std::invalid_argument("Possible corruption: number of levels " + std::to_string(num_levels)
+ (raw_items ? " with raw items" : ""));
}
Expand Down
20 changes: 20 additions & 0 deletions req/test/req_sketch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,26 @@ TEST_CASE("req sketch: deserialize rejects inconsistent compactors", "[req_sketc
check_rejected(bytes);
}

SECTION("too many levels") {
auto with_levels = [&](uint8_t levels) -> vector_bytes {
auto bytes = estimation.serialize();
std::vector<uint8_t> header(bytes.begin() + estimation_compactor, bytes.begin() + estimation_compactor + items);
put_value(header, num_items, static_cast<uint32_t>(0));
for (uint8_t h = bytes[num_levels_offset]; h < levels; ++h) {
put_value(header, lg_weight, h);
bytes.insert(bytes.end(), header.begin(), header.end());
}
bytes[num_levels_offset] = levels;
return bytes;
};
const auto bytes = with_levels(64);
REQUIRE(req_sketch<float>::deserialize(bytes.data(), bytes.size()).get_n() == estimation.get_n());
std::stringstream s(std::ios::in | std::ios::out | std::ios::binary);
s.write(reinterpret_cast<const char*>(bytes.data()), bytes.size());
REQUIRE(req_sketch<float>::deserialize(s).get_n() == estimation.get_n());
check_rejected(with_levels(65));
}

SECTION("no items") {
auto bytes = exact.serialize();
put_value(bytes, exact_compactor + num_items, static_cast<uint32_t>(0));
Expand Down
Loading