mirror of
https://github.com/storytold/effectcraft.git
synced 2026-11-19 16:45:28 +00:00
Merge pull request #3843 from arnav-hiwarkar/fix/3830-3831-ocio-lut-limits
OCIO: cap the .3dl mesh and rows before cubing them (#3830)
This commit is contained in:
2 files changed
+62
-10
No files matched your search
@@ -38,6 +38,11 @@ pub fn parse_spi3d(text: &str) -> Option<Lut> {
|
||||
|
||||
/// 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;
|
||||
@@ -48,34 +53,39 @@ pub fn parse_3dl(text: &str) -> Option<Lut> {
|
||||
}
|
||||
if l.starts_with(|c: char| c.is_ascii_alphabetic()) {
|
||||
// "Mesh <in bits> <out bits>" (Lustre header); other keywords are ignored.
|
||||
let t: Vec<&str> = l.split_whitespace().collect();
|
||||
let t: Vec<&str> = l.split_whitespace().take(3).collect();
|
||||
if t[0].eq_ignore_ascii_case("mesh") && t.len() >= 3 {
|
||||
out_bits = t[2].parse().ok();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
let v: Vec<f64> = l.split_whitespace().filter_map(|t| t.parse().ok()).collect();
|
||||
let v: Vec<f64> = l.split_whitespace().filter_map(|t| t.parse().ok()).take(max_side.max(3).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_side {
|
||||
return None;
|
||||
}
|
||||
mesh = Some(v);
|
||||
} else if v.len() == 3 {
|
||||
// One spare row while no mesh is known: a 3-entry mesh reads as a data row until the
|
||||
// count tells them apart below.
|
||||
if rows.len() >= max_side.saturating_pow(3).saturating_add(usize::from(mesh.is_none())) {
|
||||
return None;
|
||||
}
|
||||
rows.push([v[0], v[1], v[2]]);
|
||||
} else {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
let is_cube = |k: usize| {
|
||||
let n = (k as f64).cbrt().round() as usize;
|
||||
n * n * n == k
|
||||
};
|
||||
// A 3-entry input mesh (3³ lattice) looks like a data row: tell them apart by the count.
|
||||
if mesh.is_none() && !is_cube(rows.len()) && rows.len() == 28 {
|
||||
// A 3-entry input mesh (3³ lattice) looks like a data row: 28 rows are a mesh and 27.
|
||||
if mesh.is_none() && rows.len() == 28 {
|
||||
mesh = Some(rows.remove(0).to_vec());
|
||||
}
|
||||
let n = match &mesh {
|
||||
Some(m) => m.len(),
|
||||
None => (rows.len() as f64).cbrt().round() as usize,
|
||||
};
|
||||
if n < 2 || rows.len() != n * n * n {
|
||||
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));
|
||||
@@ -92,7 +102,7 @@ pub fn parse_3dl(text: &str) -> Option<Lut> {
|
||||
Some(Lut { n1: 0, d1: vec![], n3: n, d3, dmin: [0.0; 3], dmax: [1.0; 3] })
|
||||
}
|
||||
|
||||
/// Largest lattice side the lattice readers accept (`.spi3d`, `.csp`).
|
||||
/// Largest lattice side the lattice readers accept (`.spi3d`, `.csp`, `.3dl`).
|
||||
const MAX_LATTICE: usize = 129;
|
||||
/// Most entries of a 1D table (interpolated by index, so capped like the lattices: 2²⁴).
|
||||
const MAX_1D: usize = 1 << 24;
|
||||
|
||||
@@ -547,3 +547,45 @@ fn an_ocio_transform_file_is_read_again_once_it_changes() {
|
||||
assert!(Arc::ptr_eq(&inline, &load_file(BLACK).unwrap()));
|
||||
std::fs::remove_dir_all(&dir).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
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 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.
|
||||
let mut ok = String::from("0 511 1023\n");
|
||||
for k in 0..27 {
|
||||
ok += &format!("{} {} {}\n", k * 30, k * 30, k * 30);
|
||||
}
|
||||
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_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}");
|
||||
}
|
||||
// A 3-entry mesh reads as a 28th row until the count separates it, and a four-value row is
|
||||
// still refused at the smallest limit.
|
||||
assert!(lut::parse_3dl_limited(&table_3dl(3, true), 3).is_some_and(|l| l.n3 == 3));
|
||||
let four = table_3dl(2, false).replacen("7 7 7", "7 7 7 7", 1);
|
||||
assert!(lut::parse_3dl_limited(&four, 2).is_none());
|
||||
}
|
||||
Reference in new issue
Block a user