diff --git a/crates/victory-formats/src/texture.rs b/crates/victory-formats/src/texture.rs index 73ef98d..f512b10 100644 --- a/crates/victory-formats/src/texture.rs +++ b/crates/victory-formats/src/texture.rs @@ -651,7 +651,12 @@ impl Layout { } let n = self.desc.element_bytes as usize; let bytes = m.width as usize * n; - let need = (depth as usize - 1) * depth_pitch + (m.height as usize - 1) * row_pitch + bytes; + let need = (depth as usize - 1) + .checked_mul(depth_pitch) + .zip((m.height as usize - 1).checked_mul(row_pitch)) + .and_then(|(depth_offset, row_offset)| depth_offset.checked_add(row_offset)) + .and_then(|offset| offset.checked_add(bytes)) + .ok_or(Error::TooSmall)?; if tiled_len < self.size as usize || linear_len < need || row_pitch < bytes { return Err(Error::TooSmall); } @@ -1010,6 +1015,37 @@ mod tests { assert!(!t.planes[0].layout.addressable()); } + #[test] + fn texture_copies_reject_overflowing_pitches() { + let layout = Layout::new(&desc(4, 2, 1, 4, Swizzle::LINEAR)).unwrap(); + let mut tiled = vec![0; layout.size as usize]; + let mut linear = [0; 32]; + assert_eq!( + layout.detile(&tiled, 0, 0, &mut linear, usize::MAX, 0), + Err(Error::TooSmall) + ); + assert_eq!( + layout.tile(&mut tiled, 0, 0, &linear, usize::MAX, 0), + Err(Error::TooSmall) + ); + } + + #[test] + fn volume_copy_rejects_overflowing_depth_pitch() { + let layout = Layout::new(&Desc { + dimension: Dimension::Tex3d, + depth: 3, + ..desc(4, 2, 1, 4, Swizzle::LINEAR) + }) + .unwrap(); + let tiled = vec![0; layout.size as usize]; + let mut linear = [0; 96]; + assert_eq!( + layout.detile(&tiled, 0, 0, &mut linear, 16, usize::MAX), + Err(Error::TooSmall) + ); + } + #[test] fn bad_descriptions_and_buffers_are_refused() { assert_eq!( diff --git a/crates/victory-formats/src/xma.rs b/crates/victory-formats/src/xma.rs index 4a9e606..757dc6a 100644 --- a/crates/victory-formats/src/xma.rs +++ b/crates/victory-formats/src/xma.rs @@ -53,7 +53,8 @@ pub struct Packet { } pub fn packet(data: &[u8], index: usize) -> Option { - let b = data.get(index * PACKET_BYTES..index * PACKET_BYTES + 4)?; + let start = index.checked_mul(PACKET_BYTES)?; + let b = data.get(start..)?.get(..4)?; let h = u32::from_be_bytes([b[0], b[1], b[2], b[3]]); Some(Packet { frames: h >> 26, @@ -1112,6 +1113,12 @@ mod tests { } } + #[test] + fn packet_rejects_out_of_range_indices_and_partial_headers() { + assert!(packet(&[0; 4], usize::MAX / PACKET_BYTES + 1).is_none()); + assert!(packet(&[0; 3], 0).is_none()); + } + #[test] fn packet_headers_and_frame_walk() { // Two packets: frame A (100 bits) and B (16300 bits, crossing into