From e3e29b11fc2908e96721eb110f39c1a3e63e6689 Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Thu, 5 Sep 2024 18:05:55 -0700 Subject: [PATCH] inv: Avoid numeric overflow on rotating `IconRow`. --- all-is-cubes-base/src/math/rotation.rs | 1 + all-is-cubes/src/inv/inv_in_block.rs | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/all-is-cubes-base/src/math/rotation.rs b/all-is-cubes-base/src/math/rotation.rs index 17bfe4670..2fe4fa28e 100644 --- a/all-is-cubes-base/src/math/rotation.rs +++ b/all-is-cubes-base/src/math/rotation.rs @@ -374,6 +374,7 @@ impl GridRotation { /// /// May panic or wrap if `vector` has any components equal to [`GridCoordinate::MIN`]. #[inline] + #[track_caller] pub fn transform_vector(self, vector: GridVector) -> GridVector { #[inline(always)] fn inner(rotation: GridRotation, vector: GridVector) -> Option { diff --git a/all-is-cubes/src/inv/inv_in_block.rs b/all-is-cubes/src/inv/inv_in_block.rs index 6ae021f8e..8b61361ea 100644 --- a/all-is-cubes/src/inv/inv_in_block.rs +++ b/all-is-cubes/src/inv/inv_in_block.rs @@ -1,7 +1,8 @@ //! Configuration of inventories owned by blocks ([`Modifier::Inventory`]). use alloc::vec::Vec; -use euclid::Point3D; + +use euclid::{Point3D, Vector3D}; use crate::block::Resolution; use crate::math::{GridCoordinate, GridPoint, GridRotation, GridVector, Gridgid}; @@ -192,7 +193,10 @@ impl IconRow { first_slot: self.first_slot, count: self.count, origin: transform.transform_point(self.origin), // TODO: does not account for size of icon - stride: transform.rotation.transform_vector(self.stride), + stride: transform.rotation.transform_vector( + // Kludge: clamped to avoid numeric overflow of `i32::MIN` + self.stride.max(Vector3D::splat(-i32::MAX)), + ), } } }