From 1eab37b677a6639aca776c562613faad814a6cb9 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 27 Sep 2020 09:38:34 -0700 Subject: [PATCH] wezterm: fix panic when a large paste is chunked in an emoji wezterm splits pastes into chunks of 1KB. If that chunk was in the middle of a UTF8 multibyte sequence, the rust string library would panic. This commit rounds the chunk size up to the next character boundary. closes: https://github.com/wez/wezterm/issues/281 --- src/mux/tab.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/mux/tab.rs b/src/mux/tab.rs index 5d11ae74b..5165eafde 100644 --- a/src/mux/tab.rs +++ b/src/mux/tab.rs @@ -45,7 +45,13 @@ fn schedule_next_paste(paste: &Arc>) { let pane = mux.get_pane(locked.pane_id).unwrap(); let remain = locked.text.len() - locked.offset; - let chunk = remain.min(PASTE_CHUNK_SIZE); + let mut chunk = remain.min(PASTE_CHUNK_SIZE); + + // Make sure we chunk at a char boundary, otherwise the + // slice operation below will panic + while !locked.text.is_char_boundary(locked.offset + chunk) && chunk < remain { + chunk += 1; + } let text_slice = &locked.text[locked.offset..locked.offset + chunk]; pane.send_paste(text_slice).unwrap();