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] 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)); + } }