From 6b09c7db800e670e819b603ce9bc8e44789338ed Mon Sep 17 00:00:00 2001 From: Davidson Souza Date: Wed, 26 Jun 2024 14:01:39 -0300 Subject: [PATCH] parent_many: force a constant to u64 By default, in 64 bits machine rustc will interpret constants as u32, but in parent_many we left shift a constant by the depth of the forest, which can be grather than 32. This would a 32-bits value to be left-shifted by more than 32. This is undefined behavour and should be avoided. This commit coerces the constants to u64, making it safe shift up to 63. Since tree with depth 63 would be impossible to see in practice (it would represent quadrilions of leaves), there's no risk of having the same UB now. --- src/accumulator/util.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/accumulator/util.rs b/src/accumulator/util.rs index a3d025b..4848248 100644 --- a/src/accumulator/util.rs +++ b/src/accumulator/util.rs @@ -233,7 +233,7 @@ pub fn parent_many(pos: u64, rise: u8, forest_rows: u8) -> Result { rise, forest_rows )); } - let mask = ((2 << forest_rows) - 1) as u64; + let mask = ((2_u64 << forest_rows) - 1) as u64; Ok((pos >> rise | (mask << (forest_rows - (rise - 1)) as u64)) & mask) }