tony / oxwm

fix: prevent crash when spawning non-existent commands Browse files
Previously, spawning a command that didn't exist would cause the wm to
crash. Commit adds proper error handling for `KeyAction::Spawn` to catch
IO's `ErrorKind::NotFound` and prints a warning instead of propagating
up the call stack.
emzywastaken committed 2025-10-12 parent 9b07b6c commit 2971bafad6e0e8dbdc3958fb410782f408c31646
Showing 2 changed files with 42 additions and 12 deletions. UnifiedSplit
+40 −0 src/keyboard/handlers.rs
@@ -1,3 +1,5 @@
1 +use std::process::Command;
2 +
1 3 use anyhow::Result;
2 4 use x11rb::connection::Connection;
3 5 use x11rb::protocol::xproto::*;
@@ -92,3 +94,41 @@ pub fn handle_key_press(event: KeyPressEvent, keybindings: &[Key]) -> Result<(Ke
92 94
93 95 Ok((KeyAction::None, Arg::None))
94 96 }
97 +
98 +pub fn handle_spawn_action(action: KeyAction, arg: &Arg) -> Result<()> {
99 + use std::io::ErrorKind;
100 + match action {
101 + KeyAction::Spawn => match arg {
102 + Arg::Str(command) => match Command::new(command).spawn() {
103 + Err(err) if err.kind() == ErrorKind::NotFound => {
104 + eprintln!(
105 + "KeyAction::Spawn failed: could not spawn \"{}\", command not found",
106 + command
107 + );
108 + }
109 + Err(err) => Err(err)?,
110 + _ => (),
111 + },
112 + Arg::Array(command) => {
113 + let Some((cmd, args)) = command.split_first() else {
114 + return Ok(());
115 + };
116 +
117 + match Command::new(cmd).args(args).spawn() {
118 + Err(err) if err.kind() == ErrorKind::NotFound => {
119 + eprintln!(
120 + "KeyAction::Spawn failed: could not spawn \"{}\", command not found",
121 + cmd
122 + );
123 + }
124 + Err(err) => Err(err)?,
125 + _ => (),
126 + }
127 + }
128 + _ => {}
129 + },
130 + _ => {}
131 + }
132 +
133 + Ok(())
134 +}
+2 −12 src/window_manager.rs
@@ -1,6 +1,6 @@
1 1 use crate::Config;
2 2 use crate::bar::Bar;
3 -use crate::keyboard::{self, Arg, KeyAction};
3 +use crate::keyboard::{self, Arg, KeyAction, handlers};
4 4 use crate::layout::GapConfig;
5 5 use crate::layout::Layout;
6 6 use crate::layout::tiling::TilingLayout;
@@ -449,17 +449,7 @@ impl WindowManager {
449 449
450 450 fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> Result<()> {
451 451 match action {
452 - KeyAction::Spawn => match arg {
453 - Arg::Str(command) => {
454 - std::process::Command::new(command).spawn()?;
455 - }
456 - Arg::Array(cmd) => {
457 - if let Some((program, args)) = cmd.split_first() {
458 - std::process::Command::new(program).args(args).spawn()?;
459 - }
460 - }
461 - _ => {}
462 - },
452 + KeyAction::Spawn => handlers::handle_spawn_action(action, arg)?,
463 453 KeyAction::KillClient => {
464 454 if let Some(focused) = self.focused_window {
465 455 match self.connection.kill_client(focused) {