From 96c3d54ac7d5f7ff87f3235dec4af66ce1d6b2fd Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Tue, 18 Oct 2022 02:55:02 +0000 Subject: [PATCH] fix: Fix power of two computation on 32bit architectures (#1624) The current `compute_previous_power_of_two()` implementation used for TermHashmap takes and returns `usize` , but actually only works correclty on 64 bit architectures (aka usize == u64) On other architectures the leading_zeros computation is run on the wrong type (must be u64), and leads to overflows. Fixed simply computing the leading_zeros based on a u64 value. --- src/postings/stacker/term_hashmap.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/postings/stacker/term_hashmap.rs b/src/postings/stacker/term_hashmap.rs index f34297093..19c29b7ca 100644 --- a/src/postings/stacker/term_hashmap.rs +++ b/src/postings/stacker/term_hashmap.rs @@ -98,7 +98,7 @@ impl<'a> Iterator for Iter<'a> { /// # Panics if n == 0 fn compute_previous_power_of_two(n: usize) -> usize { assert!(n > 0); - let msb = (63u32 - n.leading_zeros()) as u8; + let msb = (63u32 - (n as u64).leading_zeros()) as u8; 1 << msb }