Merge pull request #50 from Vanthys/fix/format-offset-overflow
Guard texture and XMA offsets against overflow
This commit is contained in:
commit
b70bb6871a
2 files changed
+48
-2
No files matched your search
@@ -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!(
|
||||
|
||||
@@ -53,7 +53,8 @@ pub struct Packet {
|
||||
}
|
||||
|
||||
pub fn packet(data: &[u8], index: usize) -> Option<Packet> {
|
||||
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
|
||||
@@ -1155,11 +1162,13 @@ mod tests {
|
||||
fn walks_saved_streams() {
|
||||
let dir = std::env::var("VICTORY_XMA_STREAMS").unwrap();
|
||||
let mut bad = 0;
|
||||
let mut streams = 0;
|
||||
for entry in std::fs::read_dir(dir).unwrap() {
|
||||
let path = entry.unwrap().path();
|
||||
if path.extension().is_none_or(|e| e != "xma") {
|
||||
continue;
|
||||
}
|
||||
streams += 1;
|
||||
let data = std::fs::read(&path).unwrap();
|
||||
let counted: u32 = (0..data.len() / PACKET_BYTES)
|
||||
.map(|i| packet(&data, i).unwrap().frames)
|
||||
@@ -1185,6 +1194,7 @@ mod tests {
|
||||
);
|
||||
bad += u32::from(walked != counted || errors > 0);
|
||||
}
|
||||
assert!(streams > 0, "VICTORY_XMA_STREAMS contains no .xma files");
|
||||
assert_eq!(bad, 0);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user