Skip to content

A self-improving RLM agent for coding workflows and long-running autonomous tasks.

Rust

reject malformed meta-wrapped CSI input with a non-ASCII char instead of panicking (#3252)

Sebastian Müller1fdc642aeaf0
diff --git a/.changes/csi-modifier-non-ascii-panic.md b/.changes/csi-modifier-non-ascii-panic.md
new file mode 100644
index 0000000000..0a7716c64d
--- /dev/null
+++ b/.changes/csi-modifier-non-ascii-panic.md
@@ -0,0 +1 @@
+- A malformed escape sequence containing a non-ASCII character (for example, Option-key input mixed with Cyrillic or accented text) is now passed through as typed text instead of crashing the terminal UI.
diff --git a/crates/pa-tui/src/input.rs b/crates/pa-tui/src/input.rs
index f22d27d2d4..3ca8c91d4e 100644
--- a/crates/pa-tui/src/input.rs
+++ b/crates/pa-tui/src/input.rs
@@ -513,10 +513,8 @@ fn decode_legacy_meta_sequence(body: &[char]) -> Option<KeyEvent> {
/// never finds a key either. Kept to the finals the product binds.
fn decode_csi_with_modifier(rest: &str) -> Option<(KeyCode, KeyModifiers)> {
let (params, last) = rest.split_once(';')?;
- if last.is_empty() {
- return None;
- }
- let (modifier, final_char) = last.split_at(last.chars().count() - 1);
+ let (final_index, _) = last.char_indices().next_back()?;
+ let (modifier, final_char) = last.split_at(final_index);
let modifiers = match modifier.parse::<u8>().ok()? {
1 => KeyModifiers::NONE,
2 => KeyModifiers::SHIFT,
@@ -1009,6 +1007,24 @@ mod tests {
assert!(matches!(incomplete[0], Event::Key(ref k) if k.code == KeyCode::Esc));
}
+ /// A malformed wrapped tail with a multi-byte character before its last
+ /// char is rejected, not a panic: the chunk passes through untouched.
+ #[test]
+ fn malformed_non_ascii_wrapped_tail_stays_untouched() {
+ let _guard = crate::enhanced_keys::TEST_STATE_LOCK
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ crate::enhanced_keys::set_kitty_active_for_tests(false);
+ let chunk = vec![
+ Event::Key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
+ press('['),
+ press(';'),
+ press('я'),
+ press(';'),
+ ];
+ assert_eq!(merge_legacy_meta_escapes(chunk.clone()), chunk);
+ }
+
/// Inactive under the kitty protocol: those terminals report
/// option-modified keys natively and never send the double-ESC form.
#[test]