From 59ce036a8f01c4930135f1375d313254beb199fb Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:10:57 +0100 Subject: [PATCH] tga, dpx: reserve the RGBA8 export buffer fallibly The caller already holds the frame, so the output is a second large buffer. Report OutOfMemory instead of letting a failed allocation abort. --- crates/dpx/src/rgba8_dpx10.rs | 7 ++++++- crates/tga/src/rgba8_raw.rs | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/crates/dpx/src/rgba8_dpx10.rs b/crates/dpx/src/rgba8_dpx10.rs index 05ce20b15..6554a1eb8 100644 --- a/crates/dpx/src/rgba8_dpx10.rs +++ b/crates/dpx/src/rgba8_dpx10.rs @@ -6,6 +6,8 @@ pub enum Rgba8EncodeError { FrameSize, /// A dimension cannot fit the output format. TooLarge, + /// The output buffer could not be allocated. + 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)?; @@ -82,7 +84,10 @@ pub fn encode_rgba8_dpx10(rgba: &[u8], w: u32, h: u32, creator: &str) -> std::re put32(&mut hd, 1632, 1); // television header: progressive hd[1928] = 0; - let mut out = Vec::with_capacity(size); + // 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)?; 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 { diff --git a/crates/tga/src/rgba8_raw.rs b/crates/tga/src/rgba8_raw.rs index acec4b9bc..40cc256c7 100644 --- a/crates/tga/src/rgba8_raw.rs +++ b/crates/tga/src/rgba8_raw.rs @@ -6,6 +6,8 @@ pub enum Rgba8EncodeError { FrameSize, /// A dimension cannot fit the 16-bit Targa header. Dimensions, + /// The output buffer could not be allocated. + 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)?; @@ -24,7 +26,11 @@ pub fn encode_rgba8_raw(rgba: &[u8], w: u32, h: u32, alpha: bool) -> std::result }; let bpp: usize = if alpha { 4 } else { 3 }; let row = w as usize * 4; - let mut out = Vec::with_capacity(18 + w as usize * h as usize * bpp + 26); + // 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)?; 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