From cd25d98bfa38c87bd857264e876cd8be42eb3678 Mon Sep 17 00:00:00 2001 From: wm4 Date: Tue, 27 Jun 2017 14:22:28 +0200 Subject: Avoid calling close(-1) While this is perfectly OK on Unix, it causes annoying valgrind warnings, and might be otherwise confusing to others. On Windows, the runtime can actually abort the process if this is called. push.c part taken from a patch by Pedro Pombeiro. --- osdep/terminal-unix.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'osdep/terminal-unix.c') diff --git a/osdep/terminal-unix.c b/osdep/terminal-unix.c index ffd6ee486c..eca6c69461 100644 --- a/osdep/terminal-unix.c +++ b/osdep/terminal-unix.c @@ -368,7 +368,16 @@ static void continue_sighandler(int signum) static pthread_t input_thread; static struct input_ctx *input_ctx; -static int death_pipe[2]; +static int death_pipe[2] = {-1, -1}; + +static void close_death_pipe(void) +{ + for (int n = 0; n < 2; n++) { + if (death_pipe[n] >= 0) + close(death_pipe[n]); + death_pipe[n] = -1; + } +} static void quit_request_sighandler(int signum) { @@ -421,8 +430,7 @@ void terminal_setup_getch(struct input_ctx *ictx) if (pthread_create(&input_thread, NULL, terminal_thread, NULL)) { input_ctx = NULL; - close(death_pipe[0]); - close(death_pipe[1]); + close_death_pipe(); return; } @@ -450,8 +458,7 @@ void terminal_uninit(void) if (input_ctx) { (void)write(death_pipe[1], &(char){0}, 1); pthread_join(input_thread, NULL); - close(death_pipe[0]); - close(death_pipe[1]); + close_death_pipe(); input_ctx = NULL; } -- cgit v1.2.3