From b8ac784ffc476511a4148bbb02f4ac2c2478fcb4 Mon Sep 17 00:00:00 2001 From: Kirill Chibisov Date: Tue, 18 Oct 2022 17:13:31 +0300 Subject: [PATCH] On Wayland, fix invalid offsets being sent in Preedit Even when the protocol explicitly tells to send proper UTF-8 boundaries for cursor, some IMEs don't do that, so sanity check them before sending downstream. --- CHANGELOG.md | 2 ++ .../linux/wayland/seat/text_input/handlers.rs | 9 +++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bbbb0e9d..c89d26596 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ And please only add new entries to the top of this list, right below the `# Unre # Unreleased +- On Wayland, fix byte offset in `Ime::Preedit` pointing to invalid bytes. + # 0.27.4 - On Windows, emit `ReceivedCharacter` events on system keybindings. diff --git a/src/platform_impl/linux/wayland/seat/text_input/handlers.rs b/src/platform_impl/linux/wayland/seat/text_input/handlers.rs index 8d12b8f4e..ed2540cb5 100644 --- a/src/platform_impl/linux/wayland/seat/text_input/handlers.rs +++ b/src/platform_impl/linux/wayland/seat/text_input/handlers.rs @@ -68,9 +68,14 @@ pub(super) fn handle_text_input( cursor_begin, cursor_end, } => { - let cursor_begin = usize::try_from(cursor_begin).ok(); - let cursor_end = usize::try_from(cursor_end).ok(); let text = text.unwrap_or_default(); + let cursor_begin = usize::try_from(cursor_begin) + .ok() + .and_then(|idx| text.is_char_boundary(idx).then(|| idx)); + let cursor_end = usize::try_from(cursor_end) + .ok() + .and_then(|idx| text.is_char_boundary(idx).then(|| idx)); + inner.pending_preedit = Some(Preedit { text, cursor_begin,