Skip to content

Commit

Permalink
Add burst_threshold limit check to the size limit of array_hash
Browse files Browse the repository at this point in the history
The array_hash used in the hash_node has a size limit of
std::numeric_limits<IndexSizeT>::max(), if more values are inserted, an
exception is thrown. Check that the burst_threshold is within this
limit beforehand so it doesn't happen.
  • Loading branch information
Tessil committed Aug 26, 2024
1 parent d98d603 commit 611bdcc
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions include/tsl/htrie_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,14 @@ class htrie_hash {
using const_prefix_iterator = htrie_hash_iterator<true, true>;

private:
using ArrayHashIndexSizeT = std::uint16_t;
using array_hash_type = typename std::conditional<
has_value<T>::value,
tsl::array_map<CharT, T, Hash, tsl::ah::str_equal<CharT>, false, KeySizeT,
std::uint16_t, tsl::ah::power_of_two_growth_policy<4>>,
ArrayHashIndexSizeT,
tsl::ah::power_of_two_growth_policy<4>>,
tsl::array_set<CharT, Hash, tsl::ah::str_equal<CharT>, false, KeySizeT,
std::uint16_t,
ArrayHashIndexSizeT,
tsl::ah::power_of_two_growth_policy<4>>>::type;

private:
Expand Down Expand Up @@ -1245,7 +1247,11 @@ class htrie_hash {

void burst_threshold(size_type threshold) {
const size_type min_burst_threshold = MIN_BURST_THRESHOLD;
m_burst_threshold = std::max(min_burst_threshold, threshold);
const size_type max_burst_threshold = MAX_BURST_THRESHOLD;

m_burst_threshold = threshold;
m_burst_threshold = std::max(m_burst_threshold, min_burst_threshold);
m_burst_threshold = std::min(m_burst_threshold, max_burst_threshold);
}

/*
Expand Down Expand Up @@ -2146,6 +2152,8 @@ class htrie_hash {

static const size_type HASH_NODE_DEFAULT_INIT_BUCKETS_COUNT = 32;
static const size_type MIN_BURST_THRESHOLD = 4;
static const size_type MAX_BURST_THRESHOLD =
std::numeric_limits<ArrayHashIndexSizeT>::max();

std::unique_ptr<anode> m_root;
size_type m_nb_elements;
Expand Down

0 comments on commit 611bdcc

Please sign in to comment.