Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Reject non-finite values when parsing .cube LUTs

NetterSilver2b518b55fe65
diff --git a/crates/pipeline/src/lut.rs b/crates/pipeline/src/lut.rs
index b66c3444e..fccb34e8f 100644
--- a/crates/pipeline/src/lut.rs
+++ b/crates/pipeline/src/lut.rs
@@ -24,7 +24,7 @@ impl Lut3d {
let mut data = Vec::new();
let three = |rest: &str| -> Option<[f32; 3]> {
let v: Vec<f32> = rest.split_whitespace().filter_map(|x| x.parse().ok()).collect();
- (v.len() == 3).then(|| [v[0], v[1], v[2]])
+ (v.len() == 3 && v.iter().all(|x| x.is_finite())).then(|| [v[0], v[1], v[2]])
};
for line in text.lines() {
let l = line.trim();
@@ -140,4 +140,19 @@ mod tests {
assert!(Lut3d::parse_cube("LUT_1D_SIZE 4\n0 0 0").is_err());
assert!(Lut3d::parse_cube("LUT_3D_SIZE 2\n0 0 0\n").is_err(), "too few entries");
}
+
+ #[test]
+ fn non_finite_entries_rejected() {
+ let bad_line = |body: &str| {
+ let e = Lut3d::parse_cube(body).expect_err(body);
+ assert!(e.starts_with("bad line"), "{e}");
+ };
+ bad_line(&format!("LUT_3D_SIZE 2\n{}", "-inf inf nan\n".repeat(8)));
+ bad_line(&format!("LUT_3D_SIZE 2\n{}", "1e39 0 0\n".repeat(8)));
+ bad_line(&format!("LUT_3D_SIZE 2\n-inf 0 0\n{}", "0 0 0\n".repeat(7)));
+ let ok = cube(|c| c, 2);
+ assert!(Lut3d::parse_cube(&ok).is_ok());
+ assert!(Lut3d::parse_cube(&format!("DOMAIN_MIN nan 0 0\n{ok}")).is_err());
+ assert!(Lut3d::parse_cube(&format!("DOMAIN_MAX inf 1 1\n{ok}")).is_err());
+ }
}