ui.click / ui.move reject coordinates beyond the screen range (#9137)

A finite x or y past f32's range became inf; on the next frame egui's
pointer delta inf - inf was NaN and its Vec2 equality assert panicked
outside the dispatch guard, closing the app. Both now lie within
+-MAX_SCREEN_POINT (1e6 points) and a larger one is an error naming the
field, with nothing queued.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
David MannandClaude Opus 5.5 committed 2026-10-24 15:41:38 +02:00
1 parent 1c3db2e5c3
commit d55964bfb3
2 files changed
+39 -5

No files matched your search

+37 -3
View File
@@ -15,7 +15,7 @@
//! - `ui.window.open {document?}` / `ui.window.close {window}`: extra document windows
//! - `ui.pointer {events: [{kind: down|move|up, x, y, pressure?, tiltX?, tiltY?, rotation?}], tool?, modifiers?, button?}`: drive the active tool in document coordinates (`button: "secondary"` opens the tool's canvas context menu (the pasteboard colour menu outside the image) or Brush Preset picker, or erases with Preferences › Tools › Right-click with painting tools = erase)
//! - `ui.click {x, y, button?, count?}` / `ui.move {x, y}`: synthetic pointer input in screen points
//! (`count` at most [`MAX_CLICKS`])
//! (`count` at most [`MAX_CLICKS`], `x` and `y` within ±[`MAX_SCREEN_POINT`])
//! - `ui.key {key, command?, shift?, alt?, ctrl?}` / `ui.type {text}`: synthetic keyboard input
//! - The synthetic input methods reject unknown fields, wrong types, unknown enum values and
//! out-of-range numbers before acting, naming the parameter (#2449)
@@ -180,6 +180,10 @@ pub const UI_SET_FIELDS: [&str; 39] = [
/// the same ceiling as the steps of one batch request.
pub const MAX_CLICKS: u64 = 256;
/// Largest `ui.click` / `ui.move` coordinate, in screen points either way (#9137). Far beyond any
/// display, yet small enough that egui's pointer delta between two such points stays finite.
pub const MAX_SCREEN_POINT: f64 = 1e6;
fn ok(v: Value) -> Outcome {
Outcome::Done(json!({"ok": true, "result": v}))
}
@@ -1183,8 +1187,9 @@ fn dispatch(app: &mut PhotocraftApp, ctx: &egui::Context, req: &ControlRequest)
let parsed = (|| -> std::result::Result<_, String> {
let p = input_object(p, None, fields)?;
// Screen coordinates in points (as reported by ui.inspect window size).
let x = input_number(&p, "x", "x", None)?.unwrap_or(0.0) as f32;
let y = input_number(&p, "y", "y", None)?.unwrap_or(0.0) as f32;
let range = Some((-MAX_SCREEN_POINT, MAX_SCREEN_POINT));
let x = input_number(&p, "x", "x", range)?.unwrap_or(0.0) as f32;
let y = input_number(&p, "y", "y", range)?.unwrap_or(0.0) as f32;
let button = match input_choice(&p, "button", "button", &BUTTONS)?.unwrap_or("left") {
"right" | "secondary" => egui::PointerButton::Secondary,
"middle" => egui::PointerButton::Middle,
@@ -3128,6 +3133,35 @@ mod tests {
}
}
/// #9137: a coordinate past f32's range became `inf`, and on the next frame egui's pointer
/// delta `inf - inf` panicked outside the dispatch guard, closing the app.
#[test]
fn huge_click_coordinate_is_rejected() {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
let ctx = egui::Context::default();
for method in ["ui.click", "ui.move"] {
for (field, v) in [("x", 1e308), ("y", -1e308), ("x", 3.5e38), ("y", 1e7), ("x", -1.5e6)] {
let (req, _rx) = ControlRequest::new(method, json!({ field: v }));
let Outcome::Done(r) = handle(&mut app, &ctx, &req) else { panic!("{method} {field}={v}: accepted") };
assert_eq!(r["ok"], false, "{method} {field}={v}: {r}");
assert!(r["error"].as_str().unwrap().starts_with(&format!("{field} must be between")), "{r}");
assert!(app.synthetic.is_empty(), "a rejected {method} queues nothing");
}
}
// Points at the edges of the range, off-window included, reach egui and run a frame each.
let edge = MAX_SCREEN_POINT;
for (method, x, y) in [("ui.move", edge, -edge), ("ui.click", -edge, edge), ("ui.click", -5.0, 20.0)] {
let (req, _rx) = ControlRequest::new(method, json!({"x": x, "y": y}));
assert!(matches!(handle(&mut app, &ctx, &req), Outcome::AfterInput));
while !app.synthetic.is_empty() {
let events = app.take_synthetic_step();
ctx.run_ui(egui::RawInput { events, ..Default::default() }, |_| {}).textures_delta.clear();
}
let pos = ctx.input(|i| i.pointer.latest_pos());
assert_eq!(pos, Some(egui::pos2(x as f32, y as f32)), "{method} {x},{y}");
}
}
/// #2449: the error of a rejected synthetic input call, after checking it changed nothing.
fn rejected(app: &mut PhotocraftApp, ctx: &egui::Context, method: &str, params: Value, names: &str) -> String {
let (tool, revision) = (app.ui.tool, app.session.active().map(|d| d.revision));
+2 -2
View File
@@ -80,9 +80,9 @@ The transport is `apps/photocraft/src/control_server.rs`, and the handlers are i
- Magnetic Lasso (`tool: "magneticLasso"`): a `down`/`up` pair is a click. The first sets the first fastening point (its modifiers set the selection mode); later clicks fasten a point, or close the border on the first point. `move` events trace the border with or without a button held, fastening points by themselves; with `alt: true` a click draws a straight segment and a drag a freehand one. `ui.inspect` shows the border in progress under `magnetic` (`path`, `anchors`, `live`, `mode`). `ui.key` Enter closes it along the edges, Backspace removes the last fastening point, Escape cancels. To select along edges in one call instead, use the `select.magneticLasso` command with rough points around the shape.
- Image › Adjustments › Curves on top, no eyedropper armed (#7325): a `down` with `command: true` adds a point on the curve the dialog shows (its Channel popup), at the pixel's tone (RGB: Rec. 709 luma; Gray: the mean) and at the curve's current output there, so the preview does not change; with `shift: true` as well, one point on each of Red, Green and Blue at that channel's value. It samples the merged image like the eyedroppers, never moves an existing point (a point already within 2 levels is selected instead, a 16-point curve takes no more), and the tool is not driven. Nothing is added with a pencil-drawn curve, in Lab or CMYK, or with a mask, saved channel or Quick Mask targeted.
- Levels or Curves adjustment layer, Properties eyedropper armed (`layerPicker` in `ui.inspect`: `{layer, point, tool}`, set by the panel's Set Black/Neutral Gray/White Point buttons): a `down` sets that point from the image below the layer as one `layer.setAdjustment {"layer", "eyedropper": {"point", "at": [x, y], "size"}}` (send that command directly to skip the UI) and the tool is not driven; Esc, another tool or another layer disarms it.
- `ui.click {x, y, button?, count?}` / `ui.move {x, y}`: synthetic pointer input in screen points (`button`: `left` (default), `right` (or `secondary`) or `middle`). `count` (default 1, `2` for a double-click) is at most 256; a larger one is an error and queues nothing
- `ui.click {x, y, button?, count?}` / `ui.move {x, y}`: synthetic pointer input in screen points (`button`: `left` (default), `right` (or `secondary`) or `middle`). `count` (default 1, `2` for a double-click) is at most 256; a larger one is an error and queues nothing. `x` and `y` lie within ±1,000,000 points (off-window is fine); a larger magnitude is an error naming the field
- `ui.key {key, command?, shift?, alt?, ctrl?}` (flags may also be grouped under `modifiers`): press and release a key, e.g. `{"key": "ArrowLeft", "shift": true}`. `{"key": "AltLeft"}` is a tap on Alt, which puts the keyboard on the in-window menu bar (then `ArrowRight`, `ArrowDown`, `Enter`, `Escape` or a menu's letter drive it); `{"key": "F", "alt": true}` opens the menu with that mnemonic (#4988)
- `ui.pointer`, `ui.click`, `ui.move`, `ui.key` and `ui.screenshot` validate every parameter before acting, as `ui.set` does: an unknown field (top-level, under `modifiers` or in an event), a value of the wrong JSON type (`"x": "abc"`, `"ctrl": "yes"`), a `button`, `kind`, `tool` or `key` outside the accepted set, or a number outside its range (`pressure`, tilt, `rotation`, `count`) is an error naming the parameter, and nothing is queued or applied. A `null` field counts as omitted and takes its default
- `ui.pointer`, `ui.click`, `ui.move`, `ui.key` and `ui.screenshot` validate every parameter before acting, as `ui.set` does: an unknown field (top-level, under `modifiers` or in an event), a value of the wrong JSON type (`"x": "abc"`, `"ctrl": "yes"`), a `button`, `kind`, `tool` or `key` outside the accepted set, or a number outside its range (`pressure`, tilt, `rotation`, `count`, the `ui.click`/`ui.move` `x`/`y`) is an error naming the parameter, and nothing is queued or applied. A `null` field counts as omitted and takes its default
- Engine commands (`engine.execute`, `photocraft-cli run`, MCP `command_run`) reject a param key their `params` doc does not name for the commands listed in `STRICT` (`crates/engine/src/param_check.rs`, today `layer.hideLayers` and `layer.showLayers`): `{"ids":[4]}` is the error `unknown parameter \`ids\` (expected: layer)`, and nothing runs. Other commands still ignore unknown keys until their `params` doc is complete and they are added to that list (`coalesce` and `target` are always accepted).
- `ui.type {text}`: type text (goes to the focused widget, or to the canvas while the Type tool is editing)
- `ui.resize {width, height}`: resize the main window