From f4a7ae21ad011940d2ad4aab1497010c2f997aed Mon Sep 17 00:00:00 2001 From: Pedro Fortunato Date: Mon, 23 Nov 2026 23:29:52 -0300 Subject: [PATCH] fix(formats): reject overlapping texture depth slices (cherry picked from commit 93a24b7cdaeb0e7f132f711e9cbd9ba405b0f639) --- crates/victory-formats/src/texture.rs | 89 ++++++++++++++++++++++++++- 1 file changed, 86 insertions(+), 3 deletions(-) diff --git a/crates/victory-formats/src/texture.rs b/crates/victory-formats/src/texture.rs index fa719c9..450360c 100644 --- a/crates/victory-formats/src/texture.rs +++ b/crates/victory-formats/src/texture.rs @@ -639,11 +639,19 @@ impl Layout { } let n = self.desc.element_bytes as usize; let bytes = m.width as usize * n; + let slice_bytes = (m.height as usize - 1) + .checked_mul(row_pitch) + .and_then(|offset| offset.checked_add(bytes)) + .ok_or(Error::TooSmall)?; + + // Multiple depth slices must not overwrite each other. + if depth > 1 && depth_pitch < slice_bytes { + return Err(Error::TooSmall); + } + 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)) + .and_then(|offset| offset.checked_add(slice_bytes)) .ok_or(Error::TooSmall)?; let (tiled_len, linear_len) = if DETILE { (src.len(), dst.len()) @@ -1138,6 +1146,81 @@ mod tests { ); } + fn volume_pitch_layout(swizzle: Swizzle) -> Layout { + Layout::new(&Desc { + dimension: Dimension::Tex3d, + depth: 2, + ..desc(4, 2, 1, 4, swizzle) + }) + .unwrap() + } + + #[test] + fn depth_pitch_rejects_overlapping_detile() { + for swizzle in [Swizzle::LINEAR, Swizzle::SW_4KB_S] { + let l = volume_pitch_layout(swizzle); + let tiled = vec![0x42; l.size as usize]; + let mut linear = [0xcc; 72]; + + for pitch in [0, 16, 35] { + assert_eq!( + l.detile(&tiled, 0, 0, &mut linear, 20, pitch), + Err(Error::TooSmall), + "{swizzle:?}, pitch {pitch}" + ); + + assert_eq!(linear, [0xcc; 72]); + } + } + } + + #[test] + fn depth_pitch_rejects_overlapping_tile() { + for swizzle in [Swizzle::LINEAR, Swizzle::SW_4KB_S] { + let l = volume_pitch_layout(swizzle); + let mut tiled = vec![0xcc; l.size as usize]; + let linear = [0x42; 72]; + + for pitch in [0, 16, 35] { + assert_eq!( + l.tile(&mut tiled, 0, 0, &linear, 20, pitch), + Err(Error::TooSmall), + "{swizzle:?}, pitch {pitch}" + ); + + assert!(tiled.iter().all(|&b| b == 0xcc)); + } + } + } + + #[test] + fn depth_pitch_accepts_exact_and_padded_slices() { + for swizzle in [Swizzle::LINEAR, Swizzle::SW_4KB_S] { + let l = volume_pitch_layout(swizzle); + + for pitch in [36usize, 40, 64] { + let len = pitch + 36; + let mut tiled = vec![0; l.size as usize]; + + let linear: Vec = (0..len).map(|i| i as u8).collect(); + + let mut back = vec![0xcc; len]; + + l.tile(&mut tiled, 0, 0, &linear, 20, pitch).unwrap(); + + l.detile(&tiled, 0, 0, &mut back, 20, pitch).unwrap(); + + for z in 0..2 { + for y in 0..2 { + let start = z * pitch + y * 20; + + assert_eq!(back[start..start + 16], linear[start..start + 16]); + } + } + } + } + } + #[test] fn volume_copy_rejects_overflowing_depth_pitch() { let layout = Layout::new(&Desc {