From a65a1f746ff41305f395801d6d4ae130b36797c0 Mon Sep 17 00:00:00 2001 From: arnav-hiwarkar Date: Tue, 17 Nov 2026 22:11:34 +0530 Subject: [PATCH] #3830: cap the .3dl mesh and rows and cube with checked math; #3831: refuse a .spi3d short of its rows before the lattice is reserved --- crates/effects/src/ocio/lut.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/crates/effects/src/ocio/lut.rs b/crates/effects/src/ocio/lut.rs index 4618a4da0..fc16da0ea 100644 --- a/crates/effects/src/ocio/lut.rs +++ b/crates/effects/src/ocio/lut.rs @@ -32,10 +32,24 @@ pub fn parse_spi1d(text: &str) -> Option { /// 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 { + 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::().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 { let mut mesh: Option> = None; @@ -56,8 +70,15 @@ pub fn parse_3dl(text: &str) -> Option { } let v: Vec = l.split_whitespace().filter_map(|t| t.parse().ok()).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 { + return None; + } mesh = Some(v); } else if v.len() == 3 { + if rows.len() >= MAX_LATTICE.pow(3) { + return None; + } rows.push([v[0], v[1], v[2]]); } else { return None; @@ -75,7 +96,7 @@ pub fn parse_3dl(text: &str) -> Option { Some(m) => m.len(), None => (rows.len() as f64).cbrt().round() as usize, }; - if n < 2 || rows.len() != n * n * n { + if !(2..=MAX_LATTICE).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 +113,7 @@ pub fn parse_3dl(text: &str) -> Option { 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;