From c68d59797e7975b2367ed37c370f4e2ae215baca Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Mon, 9 Aug 2021 22:19:29 +0200 Subject: [PATCH] fix(detect): fix possible undefined behavior --- espanso-inject/src/evdev/keymap.rs | 39 +++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/espanso-inject/src/evdev/keymap.rs b/espanso-inject/src/evdev/keymap.rs index 437b9bc..bd4aa4a 100644 --- a/espanso-inject/src/evdev/keymap.rs +++ b/espanso-inject/src/evdev/keymap.rs @@ -24,10 +24,9 @@ pub struct Keymap { impl Keymap { pub fn new(context: &Context, rmlvo: Option) -> Result { - let names = rmlvo.map(|rmlvo| Self::generate_names(rmlvo)); - - let names_ptr = names.map_or(std::ptr::null(), |names| &names); + let names = rmlvo.map(Self::generate_names); + let names_ptr = names.map_or(std::ptr::null(), |(names, _owned)| &names); let raw_keymap = unsafe { xkb_keymap_new_from_names(context.get_handle(), names_ptr, XKB_KEYMAP_COMPILE_NO_FLAGS) }; @@ -48,7 +47,7 @@ impl Keymap { self.keymap } - fn generate_names(rmlvo: KeyboardConfig) -> xkb_rule_names { + fn generate_names(rmlvo: KeyboardConfig) -> (xkb_rule_names, OwnedRawKeyboardConfig) { let rules = rmlvo .rules .map(|s| CString::new(s).expect("unable to create CString for keymap")); @@ -65,13 +64,23 @@ impl Keymap { .options .map(|s| CString::new(s).expect("unable to create CString for keymap")); - xkb_rule_names { - rules: rules.map_or(std::ptr::null(), |s| s.as_ptr()), - model: model.map_or(std::ptr::null(), |s| s.as_ptr()), - layout: layout.map_or(std::ptr::null(), |s| s.as_ptr()), - variant: variant.map_or(std::ptr::null(), |s| s.as_ptr()), - options: options.map_or(std::ptr::null(), |s| s.as_ptr()), - } + let owned_config = OwnedRawKeyboardConfig { + rules, + model, + layout, + variant, + options, + }; + + let xkb_config = xkb_rule_names { + rules: owned_config.rules.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()), + model: owned_config.model.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()), + layout: owned_config.layout.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()), + variant: owned_config.variant.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()), + options: owned_config.options.as_ref().map_or(std::ptr::null(), |s| s.as_ptr()), + }; + + (xkb_config, owned_config) } } @@ -88,3 +97,11 @@ pub enum KeymapError { #[error("could not create xkb keymap")] FailedCreation(), } + +struct OwnedRawKeyboardConfig { + rules: Option, + model: Option, + layout: Option, + variant: Option, + options: Option, +} \ No newline at end of file