Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

IO: abr fit and gradient eval check their own bounds (#5218)

Valentin Klein74812f4874e7
diff --git a/crates/io/src/abr_map.rs b/crates/io/src/abr_map.rs
index c21800f13..53cfd205f 100644
--- a/crates/io/src/abr_map.rs
+++ b/crates/io/src/abr_map.rs
@@ -527,10 +527,17 @@ fn fit(w: u32, h: u32, v: Vec<f32>, max: u32) -> (u32, u32, Vec<f32>) {
let k = (w.max(h) as f32 / max as f32).max(1.0);
let (nw, nh) = (((w as f32 / k).round() as u32).clamp(1, max), ((h as f32 / k).round() as u32).clamp(1, max));
let mut out = vec![0.0f32; nw as usize * nh as usize];
+ // Source rows/columns `[i * n / m, (i + 1) * n / m)` (at least one) for output index `i` of
+ // `m`; in u64, as `(i + 1) * n` passes u32 for a plane over 2^32 / `max` long. The parsers'
+ // edge caps keep today's inputs below that, but this function should not depend on them.
+ let span = |i: u32, n: u32, m: u32| {
+ let at = |k: u32| usize::try_from(u64::from(k) * u64::from(n) / u64::from(m)).unwrap_or(usize::MAX);
+ (at(i), at(i + 1).max(at(i).saturating_add(1)).min(n as usize))
+ };
for y in 0..nh {
- let (y0, y1) = ((y * h / nh) as usize, (((y + 1) * h / nh) as usize).max((y * h / nh) as usize + 1).min(h as usize));
+ let (y0, y1) = span(y, h, nh);
for x in 0..nw {
- let (x0, x1) = ((x * w / nw) as usize, (((x + 1) * w / nw) as usize).max((x * w / nw) as usize + 1).min(w as usize));
+ let (x0, x1) = span(x, w, nw);
let (mut sum, mut n) = (0.0, 0usize);
for yy in y0..y1 {
for xx in x0..x1 {
@@ -593,3 +600,18 @@ fn pattern_tile(p: &PsdPattern) -> Option<GrayTile> {
#[cfg(test)]
mod tests;
+
+#[cfg(test)]
+mod fit_tests {
+ use super::fit;
+
+ /// A tall, narrow plane: `(y + 1) * h` passes `u32::MAX` once `h` is over 2^32 / `max` rows.
+ /// The ABR and pattern parsers cap edges well below that, but `fit` must not rely on it.
+ #[test]
+ fn fit_tall_planes_without_overflow() {
+ let h = 5_000_000u32;
+ let (w, nh, out) = fit(1, h, vec![1.0; h as usize], 1024);
+ assert_eq!((w, nh), (1, 1024));
+ assert!(out.iter().all(|v| (*v - 1.0).abs() < 1e-6), "every output row averages real input rows");
+ }
+}
diff --git a/crates/io/src/gradient_bake.rs b/crates/io/src/gradient_bake.rs
index e452ae9e1..0d34816a7 100644
--- a/crates/io/src/gradient_bake.rs
+++ b/crates/io/src/gradient_bake.rs
@@ -106,12 +106,15 @@ fn smooth_ease(u: f32) -> f32 {
/// Colour at `t` for sorted stops (positions in 0..1) in `m`'s space.
fn eval(stops: &[(f32, [f32; 3])], mids: &[f32], smooth: f32, t: f32, m: Method) -> [f32; 3] {
let n = stops.len();
- if t <= stops[0].0 {
- return stops[0].1;
+ let (Some(first), Some(last)) = (stops.first(), stops.last()) else { return [0.0; 3] };
+ // A lone stop (even at a NaN position, which neither test below catches) is the colour.
+ if t <= first.0 || n < 2 {
+ return first.1;
}
- if t >= stops[n - 1].0 {
- return stops[n - 1].1;
+ if t >= last.0 {
+ return last.1;
}
+ // From here `stops[i + 1]` and the tangents' `n - 2` are in range: `i < n - 1` and `n >= 2`.
let i = stops.windows(2).position(|w| t >= w[0].0 && t <= w[1].0).unwrap_or(0);
let (t0, t1) = (stops[i].0, stops[i + 1].0);
let h = (t1 - t0).max(1e-6);
@@ -281,4 +284,14 @@ mod tests {
let s = bake(vec![(0.0, Color::BLACK), (1.0, Color::WHITE)], &[0.25], 0.0, Method::Classic);
assert!((at(&s, 0.25)[0] - 0.5).abs() < 0.01);
}
+
+ /// `eval` guards its own stops instead of relying on `bake`'s two-stop check.
+ #[test]
+ fn eval_with_fewer_than_two_stops_does_not_index_out_of_range() {
+ assert_eq!(eval(&[], &[], 1.0, 0.5, Method::Smooth), [0.0; 3]);
+ let lone = [(f32::NAN, [0.25, 0.5, 0.75])];
+ for m in [Method::Classic, Method::Smooth] {
+ assert_eq!(eval(&lone, &[], 1.0, 0.5, m), [0.25, 0.5, 0.75]);
+ }
+ }
}