Skip to content

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

fix(photocraft): reject an unparseable --control port instead of silently dropping it (#701) (#777)

bfentyf7be9fbe2ce7
diff --git a/apps/photocraft/src/main.rs b/apps/photocraft/src/main.rs
index 8147d6224..f7889ca24 100644
--- a/apps/photocraft/src/main.rs
+++ b/apps/photocraft/src/main.rs
@@ -66,9 +66,58 @@ fn native_options() -> eframe::NativeOptions {
}
}
+/// Parse a `--control` / `PHOTOCRAFT_CONTROL_PORT` value. An unparseable port is an error that
+/// names the value (issue #701): silently running with no control server leaves a launcher or
+/// agent unable to tell a typo from a successful grant.
+fn parse_control_port(value: &str, source: &str) -> Result<u16, String> {
+ value.trim().parse().map_err(|_| format!("{source}: `{value}` is not a valid port (expected a number from 0 to 65535)"))
+}
+
+/// Process exit status for the collected `--control` / `PHOTOCRAFT_CONTROL_PORT` errors: `None`
+/// when there are none, else 2 (a command-line usage error), never 0.
+fn control_args_exit_code(errors: &[String]) -> Option<i32> {
+ if errors.is_empty() { None } else { Some(2) }
+}
+
+#[cfg(test)]
+mod control_port_tests {
+ use super::{control_args_exit_code, parse_control_port};
+
+ #[test]
+ fn control_port_errors_exit_non_zero() {
+ assert_eq!(control_args_exit_code(&[]), None);
+ let errors = vec![parse_control_port("nope", "--control").unwrap_err()];
+ assert_eq!(control_args_exit_code(&errors), Some(2));
+ }
+
+ #[test]
+ fn control_port_accepts_valid_numbers() {
+ assert_eq!(parse_control_port("50494", "--control"), Ok(50494));
+ assert_eq!(parse_control_port(" 8080 ", "--control"), Ok(8080));
+ assert_eq!(parse_control_port("0", "PHOTOCRAFT_CONTROL_PORT"), Ok(0));
+ }
+
+ #[test]
+ fn control_port_rejects_unparseable_values_naming_them() {
+ // Out of range, a following flag eaten as the value, and an empty value all fail,
+ // naming the offending value in the message.
+ let err = parse_control_port("78787", "--control").unwrap_err();
+ assert!(err.contains("78787"), "{err}");
+ assert!(parse_control_port("--safe-gpu", "--control").is_err());
+ assert!(parse_control_port("", "--control").is_err());
+ }
+}
+
fn main() -> eframe::Result {
crash_guard::install_hook();
- let mut control_port: Option<u16> = std::env::var("PHOTOCRAFT_CONTROL_PORT").ok().and_then(|p| p.parse().ok());
+ let mut control_port: Option<u16> = None;
+ let mut control_arg_errors: Vec<String> = Vec::new();
+ if let Some(value) = std::env::var("PHOTOCRAFT_CONTROL_PORT").ok().filter(|v| !v.trim().is_empty()) {
+ match parse_control_port(&value, "PHOTOCRAFT_CONTROL_PORT") {
+ Ok(port) => control_port = Some(port),
+ Err(error) => control_arg_errors.push(error),
+ }
+ }
let mut control_token = None;
let mut control_token_file = None;
let mut automation_read_root = std::env::var_os("PHOTOCRAFT_AUTOMATION_READ_ROOT").map(std::path::PathBuf::from);
@@ -78,7 +127,13 @@ fn main() -> eframe::Result {
let mut args = std::env::args().skip(1);
while let Some(a) = args.next() {
match a.as_str() {
- "--control" => control_port = args.next().and_then(|p| p.parse().ok()),
+ "--control" => match args.next() {
+ Some(value) => match parse_control_port(&value, "--control") {
+ Ok(port) => control_port = Some(port),
+ Err(error) => control_arg_errors.push(error),
+ },
+ None => control_arg_errors.push("--control: missing port value (expected `--control <port>`)".to_string()),
+ },
"--control-token" => control_token = args.next(),
"--control-token-file" => control_token_file = args.next().map(std::path::PathBuf::from),
"--automation-read-root" => automation_read_root = args.next().map(std::path::PathBuf::from),
@@ -94,6 +149,16 @@ fn main() -> eframe::Result {
}
}
+ // A malformed control port must not silently drop the control server (issue #701): name the
+ // bad value and fail the launch, like `photocraft-cli serve --port` does for the same typo.
+ // Exit status 2 is the usual command-line usage error, so a launcher sees the failure.
+ if let Some(code) = control_args_exit_code(&control_arg_errors) {
+ for error in &control_arg_errors {
+ eprintln!("photocraft: {error}");
+ }
+ std::process::exit(code);
+ }
+
// winit and wgpu dlopen the windowing and GPU libraries, and some of those crates panic when
// one is missing (issue #201). Name the package to install and exit instead.
#[cfg(target_os = "linux")]