From beefac9c6a9f66867c9cf637f1a878c2d6316896 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Tue, 20 Feb 2018 11:18:02 -0800 Subject: [PATCH] curses backends: don't crash on weird input --- src/backend/curses/n.rs | 10 ++++++---- src/backend/curses/pan.rs | 22 ++++++++++++---------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/backend/curses/n.rs b/src/backend/curses/n.rs index 53a9f6e..9624a89 100644 --- a/src/backend/curses/n.rs +++ b/src/backend/curses/n.rs @@ -266,10 +266,12 @@ impl backend::Backend for Concrete { // Is it a UTF-8 starting point? if 32 <= ch && ch <= 255 && ch != 127 { - Event::Char( - utf8::read_char(ch as u8, || Some(ncurses::getch() as u8)) - .unwrap(), - ) + utf8::read_char(ch as u8, || Some(ncurses::getch() as u8)) + .map(Event::Char) + .unwrap_or_else(|e| { + warn!("Error reading input: {}", e); + Event::Unknown(vec![ch as u8]) + }) } else { self.parse_ncurses_char(ch) } diff --git a/src/backend/curses/pan.rs b/src/backend/curses/pan.rs index 48cb436..186cf29 100644 --- a/src/backend/curses/pan.rs +++ b/src/backend/curses/pan.rs @@ -241,16 +241,18 @@ impl backend::Backend for Concrete { pancurses::Input::Character(c) if 32 <= (c as u32) && (c as u32) <= 255 => { - Event::Char( - utf8::read_char(c as u8, || { - self.window.getch().and_then(|i| match i { - pancurses::Input::Character(c) => { - Some(c as u8) - } - _ => None, - }) - }).unwrap(), - ) + utf8::read_char(c as u8, || { + self.window.getch().and_then(|i| match i { + pancurses::Input::Character(c) => { + Some(c as u8) + } + _ => None, + }) + }).map(Event::Char) + .unwrap_or_else(|e| { + warn!("Error reading input: {}", e); + Event::Unknown(vec![c as u8]) + }) } pancurses::Input::Character(c) if (c as u32) <= 26 => { Event::CtrlChar((b'a' - 1 + c as u8) as char)