From 4c16228c70a077510628319a0466c4902b34a834 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Tue, 21 Sep 2021 21:44:46 +0200 Subject: Fix crash when X11 is connected via FD 0. If stdin is not connected, the X11 connection may use FD 0, which is traditionally used for STDIN. Do not attempt reads from fd 0 unless it _really_ is connected to a terminal on stdin. Reported by https://github.com/pvanstam, thanks a lot! Closes #631 Closes #632 --- src/main.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 93f02ad..4db4b36 100644 --- a/src/main.c +++ b/src/main.c @@ -158,8 +158,9 @@ int feh_main_iteration(int block) FD_ZERO(&fdset); FD_SET(xfd, &fdset); - if (control_via_stdin) + if (control_via_stdin) { FD_SET(STDIN_FILENO, &fdset); + } #ifdef HAVE_INOTIFY if (opt.auto_reload) { FD_SET(opt.inotify_fd, &fdset); @@ -210,7 +211,12 @@ int feh_main_iteration(int block) in that */ feh_handle_timer(); } - else if ((count > 0) && (FD_ISSET(STDIN_FILENO, &fdset))) + /* + * Beware: If stdin is not connected, we may end up with xfd == 0. + * However, STDIN_FILENO == 0 holds as well in most cases. So we must + * check control_via_stdin to avoid mistaking an X11 event for stdin. + */ + else if ((count > 0) && control_via_stdin && (FD_ISSET(STDIN_FILENO, &fdset))) feh_event_handle_stdin(); #ifdef HAVE_INOTIFY else if ((count > 0) && (FD_ISSET(opt.inotify_fd, &fdset))) @@ -227,7 +233,7 @@ int feh_main_iteration(int block) && ((errno == ENOMEM) || (errno == EINVAL) || (errno == EBADF))) eprintf("Connection to X display lost"); - else if ((count > 0) && (FD_ISSET(STDIN_FILENO, &fdset))) + else if ((count > 0) && control_via_stdin && (FD_ISSET(STDIN_FILENO, &fdset))) feh_event_handle_stdin(); #ifdef HAVE_INOTIFY else if ((count > 0) && (FD_ISSET(opt.inotify_fd, &fdset))) -- cgit v1.2.3