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: 2 additions & 0 deletions req/include/req_sketch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Compactor, AllocCompactor>& compactors);

template<typename TT = T, typename std::enable_if<std::is_floating_point<TT>::value, int>::type = 0>
static inline bool check_update_item(const TT& item) {
Expand Down
21 changes: 21 additions & 0 deletions req/include/req_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#ifndef REQ_SKETCH_IMPL_HPP_
#define REQ_SKETCH_IMPL_HPP_

#include <limits>
#include <sstream>
#include <stdexcept>
#include <type_traits>
Expand Down Expand Up @@ -513,6 +514,7 @@ req_sketch<T, C, A> req_sketch<T, C, A>::deserialize(std::istream& is, const Ser
k, static_cast<uint8_t>(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();
Expand Down Expand Up @@ -602,6 +604,7 @@ req_sketch<T, C, A> req_sketch<T, C, A>::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();
Expand All @@ -620,6 +623,24 @@ req_sketch<T, C, A> req_sketch<T, C, A>::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<typename T, typename C, typename A>
void req_sketch<T, C, A>::check_n(uint64_t n, const std::vector<Compactor, AllocCompactor>& 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();
const uint8_t lg_weight = compactor.get_lg_weight();
if (num_items > 0 && (lg_weight >= 64 || num_items > ((std::numeric_limits<uint64_t>::max() - total_weight) >> lg_weight))) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shifting 64 or more is undefined behavior in C++. So num_items == 0 case can avoid this guard.

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<typename T, typename C, typename A>
void req_sketch<T, C, A>::grow() {
const uint8_t lg_weight = get_num_levels();
Expand Down
10 changes: 10 additions & 0 deletions req/test/req_sketch_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,16 @@ 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<uint64_t>(static_cast<int64_t>(estimation.get_n()) + delta));
check_rejected(bytes);
}
}

SECTION("compactor header beyond the input") {
auto bytes = estimation.serialize();
bytes.resize(estimation_compactor + 10);
Expand Down
Loading