Skip to content

Commit 22673b0

Browse files
Copilotjll63
andcommitted
Fix potential overflow in group hash calculation by using uint32_t multiplier
Co-authored-by: jll63 <[email protected]>
1 parent 508687c commit 22673b0

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

include/boost/openmethod/policies/minimal_perfect_hash.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ struct minimal_perfect_hash : type_hash {
8888
static std::size_t shift;
8989
static std::size_t table_size; // N for minimal perfect hash
9090
static std::size_t num_groups;
91-
static std::size_t group_mult;
91+
static std::uint32_t group_mult; // Smaller type to avoid overflow
9292
static std::size_t group_shift;
9393

9494
static void check(std::size_t index, type_id type);
@@ -179,7 +179,7 @@ template<class Registry>
179179
std::size_t minimal_perfect_hash::fn<Registry>::num_groups;
180180

181181
template<class Registry>
182-
std::size_t minimal_perfect_hash::fn<Registry>::group_mult;
182+
std::uint32_t minimal_perfect_hash::fn<Registry>::group_mult;
183183

184184
template<class Registry>
185185
std::size_t minimal_perfect_hash::fn<Registry>::group_shift;
@@ -275,7 +275,10 @@ void minimal_perfect_hash::fn<Registry>::initialize(
275275
// Try different pilot hash parameters
276276
for (std::size_t pass = 0; pass < MAX_PASSES && total_attempts < MAX_ATTEMPTS; ++pass) {
277277
mult = uniform_dist(rnd) | 1;
278-
group_mult = uniform_dist(rnd) | 1;
278+
// Use a smaller multiplier for group hash to avoid overflow
279+
// We only need enough bits to distinguish between num_groups
280+
std::uniform_int_distribution<std::uint32_t> group_dist;
281+
group_mult = group_dist(rnd) | 1;
279282

280283
// Calculate M for pilot hash (number of bits for table_size range)
281284
std::size_t M = 0;

0 commit comments

Comments
 (0)