From 7d354b49dc1f77659bb2f20868a59f6d060892cb Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 10 Dec 2014 12:05:22 -0800 Subject: [PATCH] bitv: Avoid undefined behavior in mask_for_bits It was relying on the fact that x86 shifts are effectively %BITS, so shifting a u32 by 32 preserves all bits. But this is undefined behavior in general. Add an explicit %BITS to be safe. --- src/libcollections/bit.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libcollections/bit.rs b/src/libcollections/bit.rs index 25d9fbee0a76..9bc8f4128162 100644 --- a/src/libcollections/bit.rs +++ b/src/libcollections/bit.rs @@ -177,7 +177,7 @@ fn blocks_for_bits(bits: uint) -> uint { /// Computes the bitmask for the final word of the vector fn mask_for_bits(bits: uint) -> u32 { // Note especially that a perfect multiple of u32::BITS should mask all 1s. - !0u32 >> (u32::BITS - bits % u32::BITS) + !0u32 >> (u32::BITS - bits % u32::BITS) % u32::BITS } impl Bitv {