Skip to content

Commit

Permalink
inv: Avoid numeric overflow in InvInBlock::icon_positions().
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid committed Aug 5, 2024
1 parent 5843dc1 commit 80264a3
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions all-is-cubes/src/inv/inv_in_block.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Configuration of inventories owned by blocks ([`Modifier::Inventory`]).
use alloc::vec::Vec;
use euclid::Point3D;

use crate::block::Resolution;
use crate::math::{GridCoordinate, GridPoint, GridRotation, GridVector, Gridgid};
Expand Down Expand Up @@ -90,11 +91,16 @@ impl InvInBlock {
self.icon_rows.iter().flat_map(|row| {
(0..row.count).map_while(move |sub_index| {
let slot_index = row.first_slot.checked_add(sub_index)?;
Some((
slot_index,
// TODO: this should be checked arithmetic
row.origin + row.stride * GridCoordinate::try_from(sub_index).ok()?,
))
let index_coord = GridCoordinate::try_from(sub_index).ok()?;
let position: GridPoint = transpose_point_option(
row.origin
.to_vector()
.zip(row.stride, |origin_c, stride_c| {
origin_c.checked_add(stride_c.checked_mul(index_coord)?)
})
.to_point(),
)?;
Some((slot_index, position))
})
})
}
Expand Down Expand Up @@ -146,6 +152,10 @@ impl InvInBlock {
}
}

fn transpose_point_option<T, U>(v: Point3D<Option<T>, U>) -> Option<Point3D<T, U>> {
Some(Point3D::new(v.x?, v.y?, v.z?))
}

impl Default for InvInBlock {
/// Returns [`InvInBlock::EMPTY`].
fn default() -> Self {
Expand Down

0 comments on commit 80264a3

Please sign in to comment.