From 95aba513e7929abf4e6d81f4d0e8c9f709ed296c Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:17:34 +0100 Subject: [PATCH 1/2] tga, dpx: test the RGBA8 encoders' allocation-failure path Follow-up to #1631 (CodeRabbit): the fallible output reservation moves into a small helper whose OutOfMemory mapping is covered by a test. --- crates/dpx/src/rgba8_dpx10.rs | 15 +++++++++++++-- crates/tga/src/rgba8_raw.rs | 15 +++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/crates/dpx/src/rgba8_dpx10.rs b/crates/dpx/src/rgba8_dpx10.rs index 6554a1eb8..eabab2ed0 100644 --- a/crates/dpx/src/rgba8_dpx10.rs +++ b/crates/dpx/src/rgba8_dpx10.rs @@ -9,6 +9,13 @@ pub enum Rgba8EncodeError { /// The output buffer could not be allocated. OutOfMemory, } +/// An empty buffer with room for `len` bytes, or [`Rgba8EncodeError::OutOfMemory`] when the +/// allocation fails (instead of aborting the process). +fn output_buffer(len: usize) -> std::result::Result, Rgba8EncodeError> { + let mut out = Vec::new(); + out.try_reserve_exact(len).map_err(|_| Rgba8EncodeError::OutOfMemory)?; + Ok(out) +} fn checked_len(rgba: &[u8], w: u32, h: u32) -> std::result::Result { let n = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(4)).ok_or(Rgba8EncodeError::FrameSize)?; if w == 0 || h == 0 || rgba.len() != n { @@ -86,8 +93,7 @@ pub fn encode_rgba8_dpx10(rgba: &[u8], w: u32, h: u32, creator: &str) -> std::re hd[1928] = 0; // The caller already holds the frame; reserve the output fallibly so a large export reports // an error instead of aborting the process. - let mut out = Vec::new(); - out.try_reserve_exact(size).map_err(|_| Rgba8EncodeError::OutOfMemory)?; + let mut out = output_buffer(size)?; out.extend_from_slice(&hd); let ten = |v: u8| (u32::from(v) << 2) | (u32::from(v) >> 6); for &[r, g, b, _] in rgba.as_chunks::<4>().0 { @@ -107,4 +113,9 @@ mod tests { assert_eq!(&bytes[2048..], &((1023u32 << 22) | (514u32 << 2)).to_be_bytes()); assert_eq!(encode_rgba8_dpx10(&[], 0, 1, ""), Err(Rgba8EncodeError::FrameSize)); } + #[test] + fn a_failed_output_allocation_is_an_error() { + assert_eq!(output_buffer(usize::MAX), Err(Rgba8EncodeError::OutOfMemory)); + assert!(output_buffer(16).is_ok_and(|b| b.is_empty() && b.capacity() >= 16)); + } } diff --git a/crates/tga/src/rgba8_raw.rs b/crates/tga/src/rgba8_raw.rs index 40cc256c7..a671462c9 100644 --- a/crates/tga/src/rgba8_raw.rs +++ b/crates/tga/src/rgba8_raw.rs @@ -9,6 +9,13 @@ pub enum Rgba8EncodeError { /// The output buffer could not be allocated. OutOfMemory, } +/// An empty buffer with room for `len` bytes, or [`Rgba8EncodeError::OutOfMemory`] when the +/// allocation fails (instead of aborting the process). +fn output_buffer(len: usize) -> std::result::Result, Rgba8EncodeError> { + let mut out = Vec::new(); + out.try_reserve_exact(len).map_err(|_| Rgba8EncodeError::OutOfMemory)?; + Ok(out) +} fn checked_len(rgba: &[u8], w: u32, h: u32) -> std::result::Result { let n = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(4)).ok_or(Rgba8EncodeError::FrameSize)?; if w == 0 || h == 0 || rgba.len() != n { @@ -29,8 +36,7 @@ pub fn encode_rgba8_raw(rgba: &[u8], w: u32, h: u32, alpha: bool) -> std::result // The caller already holds the frame; reserve the second, output-sized buffer fallibly so a // large export reports an error instead of aborting the process. let len = (w as usize * h as usize).checked_mul(bpp).and_then(|n| n.checked_add(18 + 26)).ok_or(Rgba8EncodeError::FrameSize)?; - let mut out = Vec::new(); - out.try_reserve_exact(len).map_err(|_| Rgba8EncodeError::OutOfMemory)?; + let mut out = output_buffer(len)?; out.extend_from_slice(&[0, 0, 2]); // no ID, no colour map, uncompressed true-colour out.extend_from_slice(&[0; 5]); // colour map specification (unused) out.extend_from_slice(&[0, 0, 0, 0]); // x / y origin @@ -63,4 +69,9 @@ mod tests { assert_eq!(&bytes[bytes.len() - 18..], b"TRUEVISION-XFILE.\0"); assert_eq!(encode_rgba8_raw(&[], 0, 1, false), Err(Rgba8EncodeError::FrameSize)); } + #[test] + fn a_failed_output_allocation_is_an_error() { + assert_eq!(output_buffer(usize::MAX), Err(Rgba8EncodeError::OutOfMemory)); + assert!(output_buffer(16).is_ok_and(|b| b.is_empty() && b.capacity() >= 16)); + } } From 831c080ac579236a62b7f9e4aac8f6efe39bc349 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 02:42:09 +0100 Subject: [PATCH 2/2] dpx: write the reservation helper and its test independently of tga's --- crates/dpx/src/rgba8_dpx10.rs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/dpx/src/rgba8_dpx10.rs b/crates/dpx/src/rgba8_dpx10.rs index eabab2ed0..8dec304ec 100644 --- a/crates/dpx/src/rgba8_dpx10.rs +++ b/crates/dpx/src/rgba8_dpx10.rs @@ -9,12 +9,13 @@ pub enum Rgba8EncodeError { /// The output buffer could not be allocated. OutOfMemory, } -/// An empty buffer with room for `len` bytes, or [`Rgba8EncodeError::OutOfMemory`] when the -/// allocation fails (instead of aborting the process). -fn output_buffer(len: usize) -> std::result::Result, Rgba8EncodeError> { - let mut out = Vec::new(); - out.try_reserve_exact(len).map_err(|_| Rgba8EncodeError::OutOfMemory)?; - Ok(out) +/// Reserves the DPX output up front; a failed reservation becomes [`Rgba8EncodeError::OutOfMemory`]. +fn output_buffer(size: usize) -> std::result::Result, Rgba8EncodeError> { + let mut buf = Vec::new(); + match buf.try_reserve_exact(size) { + Ok(()) => Ok(buf), + Err(_) => Err(Rgba8EncodeError::OutOfMemory), + } } fn checked_len(rgba: &[u8], w: u32, h: u32) -> std::result::Result { let n = (w as usize).checked_mul(h as usize).and_then(|n| n.checked_mul(4)).ok_or(Rgba8EncodeError::FrameSize)?; @@ -114,8 +115,8 @@ mod tests { assert_eq!(encode_rgba8_dpx10(&[], 0, 1, ""), Err(Rgba8EncodeError::FrameSize)); } #[test] - fn a_failed_output_allocation_is_an_error() { - assert_eq!(output_buffer(usize::MAX), Err(Rgba8EncodeError::OutOfMemory)); - assert!(output_buffer(16).is_ok_and(|b| b.is_empty() && b.capacity() >= 16)); + fn output_reservation_failure_maps_to_out_of_memory() { + assert!(matches!(output_buffer(usize::MAX), Err(Rgba8EncodeError::OutOfMemory))); + assert_eq!(output_buffer(DPX_HEADER).map(|b| b.capacity() >= DPX_HEADER), Ok(true)); } }