Ticket #2244: mc-2244-infinite-loop-when-stdin-fd-got-deleted.patch

File mc-2244-infinite-loop-when-stdin-fd-got-deleted.patch, 2.2 KB (added by and, 3 years ago)
  • lib/tty/tty-ncurses.c

    From c93fe773d8ca6420c4721cd988596601979186c9 Mon Sep 17 00:00:00 2001
    From: Andreas Mohr <and@gmx.li>
    Date: Fri, 25 Dec 2015 17:31:15 +0000
    Subject: [PATCH] fix #2244 infinite loop when stdin fd got deleted
    
    under slang mc check getch() for error condition but not under ncurses yet
    
    test case reported by ginggs:
      compile mc --with-screen=ncurses (does not occur with slang)
      subshell must be enabled and a suitable shell available (does not occur with busybox)
      open a GUI terminal (gnome-terminal, xterminal, konsole is also mentioned)
      start mc as root (sudo mc)
      close GUI terminal
    
    Signed-off-by: Andreas Mohr <and@gmx.li>
    ---
     lib/tty/tty-ncurses.c | 20 +++++++++++++++++++-
     1 file changed, 19 insertions(+), 1 deletion(-)
    
    diff --git a/lib/tty/tty-ncurses.c b/lib/tty/tty-ncurses.c
    index 83e6fe6..24eae0a 100644
    a b  
    3131 
    3232#include <config.h> 
    3333 
     34#include <errno.h> 
    3435#include <stdlib.h> 
    3536#include <stdarg.h> 
    3637#include <signal.h> 
     
    8384/* We use our own cursor coordibates to support partially visible widgets */ 
    8485static int mc_curs_row, mc_curs_col; 
    8586 
     87/* Controls error condition in tty_lowlevel_getch */ 
     88static gboolean no_ncurses_delay; 
     89 
    8690/*** file scope functions ************************************************************************/ 
    8791/* --------------------------------------------------------------------------------------------- */ 
    8892 
    tty_keypad (gboolean set) 
    326330void 
    327331tty_nodelay (gboolean set) 
    328332{ 
     333    no_ncurses_delay = set; 
    329334    nodelay (stdscr, (bool) set); 
    330335} 
    331336 
    tty_baudrate (void) 
    342347int 
    343348tty_lowlevel_getch (void) 
    344349{ 
    345     return getch (); 
     350    int c; 
     351    while ((c = getch()) == ERR) 
     352    { 
     353        /* in no-delay mode getch() returns ERR if no input waiting */ 
     354        if (no_ncurses_delay && errno == 0) 
     355            return -1; 
     356        if (errno == EINTR) 
     357            continue; 
     358        fprintf (stderr, 
     359                 "ncurses getch() returned ERR\n" 
     360                 "Assuming EOF on stdin and exiting\n"); 
     361       exit (EXIT_FAILURE); 
     362    } 
     363    return c; 
    346364} 
    347365 
    348366/* --------------------------------------------------------------------------------------------- */