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.
This commit is contained in:
FredApps committed 2026-11-16 02:17:34 +01:00
1 parent cb504733bf
commit 95aba513e7
2 files changed
+26 -4

No files matched your search

+13 -2
View File
@@ -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<Vec<u8>, 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<usize, Rgba8EncodeError> {
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));
}
}
+13 -2
View File
@@ -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<Vec<u8>, 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<usize, Rgba8EncodeError> {
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));
}
}