From 2f10f6fbe1b5e5fbf9e605e96cebd649ff019950 Mon Sep 17 00:00:00 2001 From: Ken Hoover Date: Mon, 28 Oct 2024 23:47:13 -0700 Subject: [PATCH] fix: Fix wrapping arithmetic for overflowing_add. --- src/local/room_coordinate.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/local/room_coordinate.rs b/src/local/room_coordinate.rs index 94b2e959..51dc66b2 100644 --- a/src/local/room_coordinate.rs +++ b/src/local/room_coordinate.rs @@ -463,13 +463,14 @@ impl RoomOffset { /// ); /// ``` pub fn overflowing_add(self, rhs: Self) -> (Self, bool) { + const RANGE_WIDTH: i8 = 2 * ROOM_SIZE_I8 - 1; self.assume_bounds_constraint(); rhs.assume_bounds_constraint(); let raw = self.0 + rhs.0; if raw <= -ROOM_SIZE_I8 { - (Self::new(raw + ROOM_SIZE_I8).unwrap_throw(), true) + (Self::new(raw + RANGE_WIDTH).unwrap_throw(), true) } else if raw >= ROOM_SIZE_I8 { - (Self::new(raw - ROOM_SIZE_I8).unwrap_throw(), true) + (Self::new(raw - RANGE_WIDTH).unwrap_throw(), true) } else { (Self::new(raw).unwrap_throw(), false) }