#3830: test the .3dl cap through an injectable limit; drop the local .spi3d precheck (#3831 belongs in craft-cms)

This commit is contained in:
arnav-hiwarkar committed 2026-11-18 15:27:36 +05:30
1 parent 104875a897
commit 648ae78591
2 files changed
+27 -33

No files matched your search

+9 -18
View File
@@ -32,26 +32,17 @@ pub fn parse_spi1d(text: &str) -> Option<Lut> {
/// Parse a Sony Imageworks `.spi3d` lattice: `SPILUT 1.0`, `3 3`, the sizes, then one
/// `r g b R G B` line per lattice point (indices, then the colour).
pub fn parse_spi3d(text: &str) -> Option<Lut> {
if !spi3d_rows_present(text) {
return None;
}
let value = craft_cms::sony_lut::parse_spi3d(text)?;
Some(Lut { n1: 0, d1: vec![], n3: value.size, d3: value.values, dmin: [0.0; 3], dmax: [1.0; 3] })
}
/// Whether a `.spi3d` file has at least as many rows as its header's lattice claims. The shared
/// reader reserves the whole lattice before it reads a row, so a short file is turned away
/// first (#3831). Counting stops at the claimed volume; nothing is allocated.
pub(super) fn spi3d_rows_present(text: &str) -> bool {
let mut lines = text.lines().map(str::trim).filter(|l| !l.is_empty()).skip(2);
let Some(n) = lines.next().and_then(|l| l.split_whitespace().next()?.parse::<usize>().ok()) else {
return false;
};
n.checked_pow(3).is_some_and(|count| lines.take(count).count() == count)
}
/// Parse an Autodesk `.3dl` lattice (integers, blue fastest; an optional input-mesh line).
pub fn parse_3dl(text: &str) -> Option<Lut> {
parse_3dl_limited(text, MAX_LATTICE)
}
/// [`parse_3dl`] with the largest accepted lattice side as a parameter, so tests can reach it.
pub(super) fn parse_3dl_limited(text: &str, max_side: usize) -> Option<Lut> {
let mut mesh: Option<Vec<f64>> = None;
let mut rows: Vec<[f64; 3]> = vec![];
let mut out_bits: Option<u32> = None;
@@ -68,15 +59,15 @@ pub fn parse_3dl(text: &str) -> Option<Lut> {
}
continue;
}
let v: Vec<f64> = l.split_whitespace().filter_map(|t| t.parse().ok()).take(MAX_LATTICE + 1).collect();
let v: Vec<f64> = l.split_whitespace().filter_map(|t| t.parse().ok()).take(max_side.saturating_add(1)).collect();
if v.len() > 3 && mesh.is_none() && rows.is_empty() {
// The mesh length is the lattice side: refuse one no lattice reader accepts (#3830).
if v.len() > MAX_LATTICE {
if v.len() > max_side {
return None;
}
mesh = Some(v);
} else if v.len() == 3 {
if rows.len() >= MAX_LATTICE.pow(3) {
if rows.len() >= max_side.saturating_pow(3) {
return None;
}
rows.push([v[0], v[1], v[2]]);
@@ -96,7 +87,7 @@ pub fn parse_3dl(text: &str) -> Option<Lut> {
Some(m) => m.len(),
None => (rows.len() as f64).cbrt().round() as usize,
};
if !(2..=MAX_LATTICE).contains(&n) || n.checked_pow(3) != Some(rows.len()) {
if !(2..=max_side).contains(&n) || n.checked_pow(3) != Some(rows.len()) {
return None;
}
let maxv = rows.iter().flat_map(|r| r.iter()).fold(0.0f64, |m, &v| m.max(v));
+18 -15
View File
@@ -553,9 +553,7 @@ fn a_3dl_with_a_hostile_mesh_is_refused_without_overflow() {
// #3830: 2 642 246³ is past usize::MAX, so cubing the mesh length overflowed.
let huge = "0 ".repeat(2_642_246) + "\n0 0 0\n";
assert!(parse_file(&huge, "3dl").is_none());
// A mesh longer than any lattice the readers accept, and a mesh whose rows run out.
let long = (0..130).map(|i| i.to_string()).collect::<Vec<_>>().join(" ") + "\n0 0 0\n";
assert!(parse_file(&long, "3dl").is_none());
// A mesh whose rows run out.
let short = (0..17).map(|i| (i * 64).to_string()).collect::<Vec<_>>().join(" ") + "\n0 0 0\n1 1 1\n";
assert!(parse_file(&short, "3dl").is_none());
// A normal small table still loads.
@@ -566,18 +564,23 @@ fn a_3dl_with_a_hostile_mesh_is_refused_without_overflow() {
assert!(matches!(parse_file(&ok, "3dl"), Some(FileXform::Lut { lut, .. }) if lut.n3 == 3 && lut.d3.len() == 27));
}
/// A complete `.3dl` table of side `n`, with its input mesh line or without one.
fn table_3dl(n: usize, mesh: bool) -> String {
let mut text = if mesh { (0..n).map(|i| (i * 1023 / (n - 1)).to_string()).collect::<Vec<_>>().join(" ") + "\n" } else { String::new() };
for k in 0..n * n * n {
text += &format!("{} {} {}\n", k % 1024, k % 1024, k % 1024);
}
text
}
#[test]
fn a_spi3d_without_its_rows_is_refused_before_the_lattice_is_reserved() {
// #3831: a header-only 129³ claim must not reach the reader that reserves the whole lattice.
let empty = "SPILUT 1.0\n3 3\n129 129 129\n";
assert!(!lut::spi3d_rows_present(empty));
assert!(!lut::spi3d_rows_present("SPILUT 1.0\n3 3\n2 2 2\n0 0 0 1 1 1\n"));
assert!(!lut::spi3d_rows_present("SPILUT 1.0\n3 3\n99999999999 2 2\n"));
assert!(parse_spi3d(empty).is_none());
let mut ok = String::from("SPILUT 1.0\n3 3\n2 2 2\n");
for i in 0..8 {
ok += &format!("{} {} {} 0 0 0\n", i & 1, (i >> 1) & 1, i >> 2);
fn a_3dl_lattice_is_capped_at_the_largest_side() {
// #3830: a complete table at the cap loads, and one a side larger is refused, with or without
// its mesh line (without one, row collection stops at the cap's volume).
for mesh in [true, false] {
let at = lut::parse_3dl_limited(&table_3dl(4, mesh), 4);
assert!(at.is_some_and(|l| l.n3 == 4 && l.d3.len() == 64), "mesh {mesh}");
assert!(lut::parse_3dl_limited(&table_3dl(5, mesh), 4).is_none(), "mesh {mesh}");
assert!(lut::parse_3dl_limited(&table_3dl(5, mesh), 5).is_some(), "mesh {mesh}");
}
assert!(lut::spi3d_rows_present(&ok));
assert!(parse_spi3d(&ok).is_some_and(|l| l.n3 == 2 && l.d3.len() == 8));
}