Files
craft-libs/crates/hdr/src/tests.rs
T
dotCooCoo 8a2c27ee2d craft-hdr: refuse buffers past isize::MAX bytes when limits are raised
With a raised max_pixels on wasm32, a picture could pass every check and
then ask vec! for more than isize::MAX bytes for its scan line or its
float picture, which panics with a capacity overflow instead of returning
an error.

- decode checks the byte count of the scan line and of the float picture
  against isize::MAX before allocating either, and returns
  LimitExceeded("pixel count") when one is over.
- Each header line is measured as text after invalid bytes become U+FFFD,
  and a line over isize::MAX bytes is LimitExceeded("header length").
- The resolution line reads its first four tokens without collecting the
  rest.
- The size checks take the bound as a value, and tests check them against
  the 32-bit bound without allocating.
2026-11-13 13:43:35 -08:00

199 lines
9.4 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
//! Ported from FilmCraft's Radiance HDR reader tests.
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
use super::*;
fn file(res: &str, body: &[u8]) -> Vec<u8> {
let mut v = format!("#?RADIANCE\n# test\nFORMAT=32-bit_rle_rgbe\nEXPOSURE=1.0\n\n{res}\n").into_bytes();
v.extend_from_slice(body);
v
}
fn dec(b: &[u8]) -> Result<Picture> {
decode(b, &Limits::default())
}
/// New-style RLE of one scan line, as Radiance's own writer does: per channel, runs of 3+ equal bytes.
fn rle_line(line: &[[u8; 4]]) -> Vec<u8> {
let n = line.len();
let mut out = vec![2, 2, (n >> 8) as u8, n as u8];
for c in 0..4 {
let ch: Vec<u8> = line.iter().map(|p| p[c]).collect();
let mut i = 0;
while i < n {
let run = ch[i..].iter().take(127).take_while(|&&b| b == ch[i]).count();
if run >= 3 {
out.extend([128 + run as u8, ch[i]]);
i += run;
} else {
let mut j = i + 1;
while j < n && j - i < 128 && !(j + 2 < n && ch[j] == ch[j + 1] && ch[j] == ch[j + 2]) {
j += 1;
}
out.push((j - i) as u8);
out.extend_from_slice(&ch[i..j]);
i = j;
}
}
}
out
}
#[test]
fn rgbe_values() {
assert_eq!(rgbe_to_linear([0, 0, 0, 0]), [0.0; 3]);
assert_eq!(rgbe_to_linear([128, 128, 128, 129]), [1.0; 3]);
assert_eq!(rgbe_to_linear([128, 64, 0, 136]), [128.0, 64.0, 0.0]);
assert_eq!(rgbe_to_linear([128, 128, 128, 128])[0], 0.5);
assert!(rgbe_to_linear([255, 255, 255, 255])[0].is_finite());
}
#[test]
fn flat_top_to_bottom() {
let body = [[10u8, 20, 30, 136], [1, 2, 3, 136], [4, 5, 6, 137], [7, 8, 9, 135]].concat();
let f = dec(&file("-Y 2 +X 2", &body)).unwrap();
assert_eq!((f.width, f.height), (2, 2));
// Exponent 137 scales by 2, 135 by 1/2.
assert_eq!(f.rgb, [10.0, 20.0, 30.0, 1.0, 2.0, 3.0, 8.0, 10.0, 12.0, 3.5, 4.0, 4.5]);
// The `#?RGBE` signature and CRLF header lines read the same.
let mut crlf = b"#?RGBE\r\nFORMAT=32-bit_rle_rgbe\r\n\r\n-Y 2 +X 2\r\n".to_vec();
crlf.extend_from_slice(&body);
assert_eq!(dec(&crlf).unwrap(), f);
}
#[test]
fn orientations() {
// 3 wide, 2 high, pixel value = its index in the file.
let body: Vec<u8> = (0..6u8).flat_map(|i| [i, 0, 0, 136]).collect();
let red = |res: &str| -> Vec<f32> { dec(&file(res, &body)).unwrap().rgb.chunks(3).map(|p| p[0]).collect() };
assert_eq!(red("-Y 2 +X 3"), [0., 1., 2., 3., 4., 5.]);
assert_eq!(red("+Y 2 +X 3"), [3., 4., 5., 0., 1., 2.]);
assert_eq!(red("-Y 2 -X 3"), [2., 1., 0., 5., 4., 3.]);
// Scan lines are columns: file index = x * 2 + (row, top first for -Y).
assert_eq!(red("+X 3 -Y 2"), [0., 2., 4., 1., 3., 5.]);
assert_eq!(red("-X 3 +Y 2"), [5., 3., 1., 4., 2., 0.]);
let f = dec(&file("+X 3 -Y 2", &body)).unwrap();
assert_eq!((f.width, f.height), (3, 2));
}
#[test]
fn run_length_lines() {
let w = 40usize;
let rows: Vec<Vec<[u8; 4]>> =
(0..3).map(|y| (0..w).map(|x| if x < 20 { [100, 50, 25, 130] } else { [(x * 5 + y) as u8, 7, 9, 131] }).collect()).collect();
let body: Vec<u8> = rows.iter().flat_map(|r| rle_line(r)).collect();
let f = dec(&file(&format!("-Y 3 +X {w}"), &body)).unwrap();
for (y, r) in rows.iter().enumerate() {
for (x, q) in r.iter().enumerate() {
assert_eq!(&f.rgb[(y * w + x) * 3..(y * w + x) * 3 + 3], &rgbe_to_linear(*q), "pixel {x},{y}");
}
}
}
#[test]
fn old_style_runs() {
// One pixel, then (1,1,1,3) repeats it 3 times, then a new pixel.
let body = [[5u8, 6, 7, 136], [1, 1, 1, 3], [9, 9, 9, 136]].concat();
let f = dec(&file("-Y 1 +X 5", &body)).unwrap();
assert_eq!(f.rgb.chunks(3).map(|p| p[0]).collect::<Vec<_>>(), [5.0, 5.0, 5.0, 5.0, 9.0]);
}
#[test]
fn hostile_input_is_an_error_not_a_panic() {
let ok = file("-Y 2 +X 2", &[0; 16]);
assert!(dec(&ok).is_ok());
for cut in 0..ok.len() - 1 {
// A cut anywhere is an error (the header or the pixel data is incomplete), never a panic.
assert!(dec(&ok[..cut]).is_err(), "{cut}");
}
assert_eq!(dec(b"P6 1 1 255 abc"), Err(Error::NotHdr));
assert_eq!(dec(&file("-Y 2 +X 2", &[0; 8])), Err(Error::Malformed("truncated pixel data")));
assert_eq!(dec(&file("-Y 0 +X 4", &[])), Err(Error::Malformed("empty picture")));
assert_eq!(dec(&file("-Y 2 -Y 2", &[0; 16])), Err(Error::Malformed("resolution line names one axis twice")));
assert_eq!(dec(&file("junk", &[0; 16])), Err(Error::Malformed("bad resolution line")));
assert!(matches!(dec(b"#?RADIANCE\nFORMAT=32-bit_rle_xyze\n\n-Y 1 +X 1\n\0\0\0\0"), Err(Error::Unsupported(_))));
assert!(matches!(dec(b"#?RADIANCE\nFORMAT=other\n\n-Y 1 +X 1\n\0\0\0\0"), Err(Error::Unsupported(_))));
// An RLE line announcing the wrong width, a run past the end, a run at the start of a flat line.
assert_eq!(dec(&file("-Y 1 +X 10", &[2, 2, 0, 11, 0])), Err(Error::Malformed("scan line length does not match the picture")));
assert_eq!(dec(&file("-Y 1 +X 10", &[2, 2, 0, 10, 200, 1])), Err(Error::Malformed("run past the end of the scan line")));
assert_eq!(dec(&file("-Y 1 +X 10", &[2, 2, 0, 10, 0])), Err(Error::Malformed("empty literal run")));
assert_eq!(dec(&file("-Y 1 +X 3", &[1, 1, 1, 9, 0, 0, 0, 0])), Err(Error::Malformed("run with no previous pixel")));
assert!(dec(b"#?RADIANCE").is_err());
}
#[test]
fn limits_are_checked_before_allocating() {
assert_eq!(dec(&file("-Y 99999999 +X 99999999", &[0; 16])), Err(Error::LimitExceeded("pixel count")));
assert_eq!(dec(&file("-Y 99999999999 +X 1", &[0; 16])), Err(Error::LimitExceeded("pixel count")));
// Within the pixel limit and past the coarse size check, but the scan lines are not there:
// refused by validation, before the float picture is allocated.
assert_eq!(dec(&file("-Y 67108864 +X 1", &[0; 16_384])), Err(Error::Malformed("truncated pixel data")));
let ok = file("-Y 2 +X 2", &[0; 16]);
assert!(decode(&ok, &Limits { max_pixels: 4, ..Limits::default() }).is_ok());
assert_eq!(decode(&ok, &Limits { max_pixels: 3, ..Limits::default() }), Err(Error::LimitExceeded("pixel count")));
// A header without its blank line inside the header limit.
let mut long = b"#?RADIANCE\n".to_vec();
for _ in 0..8 {
long.extend_from_slice(b"# 0123456789\n");
}
long.extend_from_slice(b"\n-Y 1 +X 1\n\0\0\0\0");
assert!(dec(&long).is_ok());
assert_eq!(decode(&long, &Limits { max_header: 64, ..Limits::default() }), Err(Error::LimitExceeded("header length")));
assert_eq!(decode(&long, &Limits { max_header: 8, ..Limits::default() }), Err(Error::Malformed("truncated header")));
assert_eq!((Limits::default().max_pixels, Limits::default().max_header), (1 << 26, 64 << 10));
}
/// `isize::MAX` on a 32-bit target such as `wasm32`.
const BOUND_32: u64 = i32::MAX as u64;
#[test]
fn buffer_lengths_respect_target_isize_bound() {
// A scan line of RGBE pixels, 4 bytes each.
assert_eq!(alloc_len(536_870_911, 4, BOUND_32), Some(536_870_911));
assert_eq!(alloc_len(536_870_912, 4, BOUND_32), None);
assert_eq!(alloc_len(u64::from(u32::MAX), 4, BOUND_32), None);
// The float picture: 3 floats of 4 bytes a pixel.
assert_eq!(alloc_len(178_956_970 * 3, 4, BOUND_32), Some(178_956_970 * 3));
assert_eq!(alloc_len(178_956_971 * 3, 4, BOUND_32), None);
// The default pixel limit fits a 32-bit target.
assert_eq!(alloc_len(Limits::default().max_pixels * 3, 4, BOUND_32), Some(3 << 26));
assert_eq!(checked_byte_len(u64::MAX, 4), None);
assert_eq!(alloc_len(u64::MAX, 1, MAX_ALLOC_BYTES), None);
}
#[test]
fn raised_pixel_limit_stops_at_the_target_bound() {
let open = Limits { max_pixels: u64::MAX, ..Limits::default() };
// 2 × 2 needs an 8-byte scan line and a 48-byte float picture.
let ok = file("-Y 2 +X 2", &[0; 16]);
assert!(decode_within(&ok, &open, 48).is_ok());
assert_eq!(decode_within(&ok, &open, 47), Err(Error::LimitExceeded("pixel count")));
// A 1 × 536,870,912 picture is past the 32-bit bound in both buffers; refused before
// anything is allocated, ahead of the scan-line validation.
let wide = file("-Y 1 +X 536870912", &[0; 131_072]);
assert_eq!(decode_within(&wide, &open, BOUND_32), Err(Error::LimitExceeded("pixel count")));
// 178,956,971 one-pixel scan lines fit the scan buffer but not the float picture.
let tall = file("-Y 178956971 +X 1", &[0; 43_692]);
assert_eq!(decode_within(&tall, &open, BOUND_32), Err(Error::LimitExceeded("pixel count")));
}
#[test]
fn header_lines_are_measured_after_lossy_conversion() {
// `#` and ten bytes that are not UTF-8: 11 bytes in the file, 31 as text.
let mut f = b"#?RADIANCE\n#".to_vec();
f.extend_from_slice(&[0xff; 10]);
f.extend_from_slice(b"\n\n-Y 1 +X 1\n\0\0\0\0");
assert_eq!(lossy_len(&f[11..22]), Some(31));
assert!(decode_within(&f, &Limits::default(), 31).is_ok());
assert_eq!(decode_within(&f, &Limits::default(), 30), Err(Error::LimitExceeded("header length")));
assert_eq!(lossy_len(b"ok \xe2\x82"), Some(6));
assert_eq!(lossy_len(b""), Some(0));
}
#[test]
fn resolution_tokens_after_the_fourth_are_ignored() {
let f = dec(&file("-Y 1 +X 1 a b c d e f", &[0; 4])).unwrap();
assert_eq!((f.width, f.height), (1, 1));
assert_eq!(dec(&file("-Y 1 +X", &[0; 4])), Err(Error::Malformed("bad resolution line")));
}