From 94ac3084e156b87869f8b336ae0a3fd752743179 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 2 Nov 2020 22:52:59 +1100 Subject: [PATCH] Handle reading from stdin returning 0 0 means stdin was closed, this is quite unexpected as we would usually get a SIGHUP, but it looks like in some rare case this happens and it leads to an infinite loop trying to handle stdin events (as it will always be readable from now on). Fixes #3557 --- src/ncurses_ui.cc | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/ncurses_ui.cc b/src/ncurses_ui.cc index 732f009f..e52748c9 100644 --- a/src/ncurses_ui.cc +++ b/src/ncurses_ui.cc @@ -316,7 +316,7 @@ bool NCursesUI::Palette::set_change_colors(bool change_colors) } static sig_atomic_t resize_pending = 0; -static sig_atomic_t sighup_raised = 0; +static sig_atomic_t stdin_closed = 0; template static void signal_handler(int) @@ -351,7 +351,7 @@ NCursesUI::NCursesUI() enable_mouse(true); set_signal_handler(SIGWINCH, &signal_handler<&resize_pending>); - set_signal_handler(SIGHUP, &signal_handler<&sighup_raised>); + set_signal_handler(SIGHUP, &signal_handler<&stdin_closed>); set_signal_handler(SIGTSTP, [](int){ NCursesUI::instance().suspend(); }); check_resize(true); @@ -577,7 +577,7 @@ void NCursesUI::check_resize(bool force) Optional NCursesUI::get_next_key() { - if (sighup_raised) + if (stdin_closed) { set_signal_handler(SIGWINCH, SIG_DFL); set_signal_handler(SIGHUP, SIG_DFL); @@ -596,9 +596,13 @@ Optional NCursesUI::get_next_key() } static auto get_char = []() -> Optional { - unsigned char c = 0; - if (fd_readable(STDIN_FILENO) and read(STDIN_FILENO, &c, 1) == 1) + if (not fd_readable(STDIN_FILENO)) + return {}; + + if (unsigned char c = 0; read(STDIN_FILENO, &c, 1) == 1) return c; + + stdin_closed = 1; return {}; };