From c8db484640bbfe662db3ea3a6e35ae5e65d186f8 Mon Sep 17 00:00:00 2001 From: link2xt Date: Sat, 15 May 2021 03:30:23 +0300 Subject: [PATCH] bounded: make sure lap does not overflow into index .wrapping_add() is not enuough to ensure this, it is possible to end up with index=1 if usize overflows. This is unlikely on 64-bit systems, but possible on 32-bit systems. --- src/bounded.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/bounded.rs b/src/bounded.rs index 7a7fb71..3507d98 100644 --- a/src/bounded.rs +++ b/src/bounded.rs @@ -106,7 +106,7 @@ impl Bounded { } else { // One lap forward, index wraps around to zero. // Set to `{ lap: lap.wrapping_add(1), mark: 0, index: 0 }`. - lap.wrapping_add(self.one_lap) + lap.wrapping_add(self.one_lap) & !(self.one_lap - 1) }; // Try moving the tail. @@ -169,7 +169,7 @@ impl Bounded { } else { // One lap forward, index wraps around to zero. // Set to `{ lap: lap.wrapping_add(1), mark: 0, index: 0 }`. - lap.wrapping_add(self.one_lap) + lap.wrapping_add(self.one_lap) & !(self.one_lap - 1) }; // Try moving the head.