Skip to content

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

image.analysis.recordMeasurements: refuse an area too large to measure instead of overflowing (#1184)

wstock3f8536868eac
diff --git a/crates/engine/src/analysis_cmds.rs b/crates/engine/src/analysis_cmds.rs
index 027e4b378..9bf9b08ab 100644
--- a/crates/engine/src/analysis_cmds.rs
+++ b/crates/engine/src/analysis_cmds.rs
@@ -510,9 +510,12 @@ fn summary(feats: &[Feature], all_values_median: f64) -> Feature {
}
}
-/// Selection mask (coverage ≥ 0.5) over `r`, or every pixel when there is no selection.
-fn selection_mask(d: &Document, r: Rect) -> Vec<bool> {
- let n = (r.width() * r.height()) as usize;
+/// Largest area Record Measurements handles in one pass: the gray render alone is 16 bytes per
+/// pixel, and the label buffer and mask come on top (#933).
+pub const MAX_MEASURE_PIXELS: u64 = 400_000_000;
+
+/// Selection mask (coverage ≥ 0.5) over `r` (`n` pixels), or every pixel when there is no selection.
+fn selection_mask(d: &Document, r: Rect, n: usize) -> Vec<bool> {
match &d.selection {
None => vec![true; n],
Some(sel) => {
@@ -536,8 +539,19 @@ fn selection_rows(d: &Document) -> Result<(Feature, Vec<Feature>)> {
if r.is_empty() {
return Err(EngineError::Other("the selection is empty".into()));
}
+ // A legal 65536×65536 document is 2^32 pixels: size everything from a checked count,
+ // and refuse what cannot be measured in one pass instead of overflowing (#933).
+ let n = u64::from(r.width()) * u64::from(r.height());
+ let Some(n) = (n <= MAX_MEASURE_PIXELS).then(|| usize::try_from(n).ok()).flatten() else {
+ return Err(EngineError::Other(format!(
+ "the area is too large to measure ({}×{} px; max {} megapixels): select a smaller region",
+ r.width(),
+ r.height(),
+ MAX_MEASURE_PIXELS / 1_000_000
+ )));
+ };
let (w, h) = (r.width() as usize, r.height() as usize);
- let mask = selection_mask(d, r);
+ let mask = selection_mask(d, r, n);
let gray = gray_image(d, r);
let range = gray_range(d.depth);
let feats = measure_features(&mask, &gray, w, h, (r.x0, r.y0), range);
@@ -906,8 +920,8 @@ fn record(s: &mut Session, p: &Value) -> Result<Value> {
x @ ("selection" | "ruler" | "count") => x,
other => return Err(bad(CMD, format!("unknown source `{other}` (auto|selection|ruler|count)"))),
};
- s.analysis.next_measurement += 1;
- let label = format!("Measurement {}", s.analysis.next_measurement);
+ // Numbered only once the rows exist, so a failed measurement does not use up a label.
+ let label = format!("Measurement {}", s.analysis.next_measurement + 1);
let now = p.get("dateTime").and_then(Value::as_str).map(str::to_string).unwrap_or_else(now_iso);
let factor = d.measurement.scale.factor();
let mut rows: Vec<Map<String, Value>> = Vec::new();
@@ -939,6 +953,7 @@ fn record(s: &mut Session, p: &Value) -> Result<Value> {
rows.push(m);
}
}
+ s.analysis.next_measurement += 1;
let keep = s.analysis.data_points.of(source).to_vec();
let mut ids = Vec::new();
for mut m in rows {
diff --git a/crates/engine/src/analysis_cmds/tests.rs b/crates/engine/src/analysis_cmds/tests.rs
index dcd6704f1..c043665b1 100644
--- a/crates/engine/src/analysis_cmds/tests.rs
+++ b/crates/engine/src/analysis_cmds/tests.rs
@@ -11,6 +11,22 @@ fn d(s: &Session) -> Arc<Document> {
s.active().unwrap().doc.clone()
}
+#[test]
+fn record_measurements_refuses_an_area_too_large_to_measure() {
+ // #933: with no selection the whole document is measured, and `width * height` overflowed
+ // u32 on a legal 65536×65536 document, panicking inside `run`.
+ let mut s = Session::new();
+ s.execute("file.new", json!({"width": 65536, "height": 65536, "background": "transparent"})).unwrap();
+ let err = s.execute("image.analysis.recordMeasurements", json!({"source": "selection"})).unwrap_err();
+ assert!(err.to_string().contains("too large to measure"), "{err}");
+ assert!(s.analysis.log.is_empty());
+ // A failed measurement does not use up a label: the next one is still "Measurement 1".
+ s.execute("file.new", json!({"width": 20, "height": 20, "background": "white"})).unwrap();
+ let r = s.execute("image.analysis.recordMeasurements", json!({"source": "selection"})).unwrap();
+ assert_eq!(r["rows"][0]["values"]["label"], "Measurement 1");
+ assert_eq!(r["rows"][0]["values"]["area"], 400.0);
+}
+
#[test]
fn measurement_scale_set_query_default_and_undo() {
let mut s = session(50, 40, 8);