Skip to content

Commit

Permalink
keyboard: accept String instead of Keymap when setting keymap
Browse files Browse the repository at this point in the history
Fixes soundness issue introduced in #1218.
  • Loading branch information
nicolasavru committed Nov 10, 2023
1 parent f6b8846 commit a027c92
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/input/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,17 +590,31 @@ impl<D: SeatHandler + 'static> KeyboardHandle<D> {
}

/// Change the [`Keymap`] used by the keyboard.
pub fn set_keymap(&self, data: &mut D, keymap: xkb::Keymap) {
///
/// The input is a keymap in XKB_KEYMAP_FORMAT_TEXT_V1 format.
pub fn set_keymap_from_string(&self, data: &mut D, keymap: String) -> Result<(), Error> {
let mut internal = self.arc.internal.lock().unwrap();
// Construct the Keymap internally instead of accepting one as input
// because libxkbcommon is not thread-safe.
let keymap = xkb::Keymap::new_from_string(
&xkb::Context::new(0),
keymap,
xkb::KEYMAP_FORMAT_TEXT_V1,
xkb::KEYMAP_COMPILE_NO_FLAGS,
)
.ok_or_else(|| {
debug!("Loading keymap from string failed");
Error::BadKeymap
})?;
self.update_xkb_state(data, &mut internal, keymap);
Ok(())
}

/// Change the [`XkbConfig`] used by the keyboard.
pub fn set_xkb_config(&self, data: &mut D, xkb_config: XkbConfig<'_>) -> Result<(), Error> {
let mut internal = self.arc.internal.lock().unwrap();

let keymap = xkb_config.compile_keymap(&internal.context).map_err(|_| {
debug!("Loading keymap failed");
debug!("Loading keymap from XkbConfig failed");
Error::BadKeymap
})?;
self.update_xkb_state(data, &mut internal, keymap);
Expand Down

0 comments on commit a027c92

Please sign in to comment.