diff --git a/req/include/req_sketch.hpp b/req/include/req_sketch.hpp index 8e3468f0..f2742aac 100755 --- a/req/include/req_sketch.hpp +++ b/req/include/req_sketch.hpp @@ -496,6 +496,8 @@ class req_sketch { static void check_num_levels(uint8_t num_levels, bool raw_items); static void check_serial_version(uint8_t serial_version); static void check_family_id(uint8_t family_id); + // in estimation mode, n is stored and must be the total weight of the retained items + static void check_n(uint64_t n, const std::vector& compactors); template::value, int>::type = 0> static inline bool check_update_item(const TT& item) { diff --git a/req/include/req_sketch_impl.hpp b/req/include/req_sketch_impl.hpp index d6f4c8e4..0452156c 100644 --- a/req/include/req_sketch_impl.hpp +++ b/req/include/req_sketch_impl.hpp @@ -20,6 +20,7 @@ #ifndef REQ_SKETCH_IMPL_HPP_ #define REQ_SKETCH_IMPL_HPP_ +#include #include #include #include @@ -513,6 +514,7 @@ req_sketch req_sketch::deserialize(std::istream& is, const Ser k, static_cast(i))); } } + if (num_levels > 1) check_n(n, compactors); if (num_levels == 1) { if (compactors[0].get_num_items() == 0) throw std::invalid_argument("Possible corruption: non-empty sketch with no items"); const auto begin = compactors[0].begin(); @@ -602,6 +604,7 @@ req_sketch req_sketch::deserialize(const void* bytes, size_t s ptr += pair.second; } } + if (num_levels > 1) check_n(n, compactors); if (num_levels == 1) { if (compactors[0].get_num_items() == 0) throw std::invalid_argument("Possible corruption: non-empty sketch with no items"); const auto begin = compactors[0].begin(); @@ -620,6 +623,26 @@ req_sketch req_sketch::deserialize(const void* bytes, size_t s return req_sketch(k, hra, n, std::move(min_item), std::move(max_item), std::move(compactors), comparator); } +template +void req_sketch::check_n(uint64_t n, const std::vector& compactors) { + // compaction keeps the total weight, so it is always n + uint64_t total_weight = 0; + for (const auto& compactor: compactors) { + const uint64_t num_items = compactor.get_num_items(); + // an empty compactor adds no weight, and its lg_weight can be 64 or more, too large to shift by + if (num_items == 0) continue; + const uint8_t lg_weight = compactor.get_lg_weight(); + if (lg_weight >= 64 || num_items > ((std::numeric_limits::max() - total_weight) >> lg_weight)) { + throw std::invalid_argument("Possible corruption: the total weight of the retained items overflows"); + } + total_weight += num_items << lg_weight; + } + if (n != total_weight) { + throw std::invalid_argument("Possible corruption: n " + std::to_string(n) + + " is not the total weight of the retained items " + std::to_string(total_weight)); + } +} + template void req_sketch::grow() { const uint8_t lg_weight = get_num_levels(); diff --git a/req/test/req_sketch_test.cpp b/req/test/req_sketch_test.cpp index 3db56247..053d3776 100755 --- a/req/test/req_sketch_test.cpp +++ b/req/test/req_sketch_test.cpp @@ -726,6 +726,32 @@ TEST_CASE("req sketch: deserialize rejects inconsistent compactors", "[req_sketc check_rejected(bytes); } + SECTION("n is not the total weight") { + const size_t n_offset = 8; // after the preamble, in estimation mode + for (const int64_t delta: {-1, 1}) { + CAPTURE(delta); + auto bytes = estimation.serialize(); + put_value(bytes, n_offset, static_cast(static_cast(estimation.get_n()) + delta)); + check_rejected(bytes); + } + } + + SECTION("empty compactors above level 63") { + // append empty compactors up to level 65, so the weight check reaches levels whose weight can't be shifted + auto bytes = estimation.serialize(); + const uint8_t levels = bytes[num_levels_offset]; + vector_bytes header(bytes.begin() + estimation_compactor, bytes.begin() + estimation_compactor + items); + put_value(header, num_items, static_cast(0)); + for (uint8_t level = levels; level < 66; ++level) { + put_value(header, lg_weight, level); + bytes.insert(bytes.end(), header.begin(), header.end()); + } + bytes[num_levels_offset] = 66; + const size_t n_offset = 8; // after the preamble, in estimation mode + put_value(bytes, n_offset, estimation.get_n() + 1); + check_rejected(bytes); + } + SECTION("compactor header beyond the input") { auto bytes = estimation.serialize(); bytes.resize(estimation_compactor + 10);