diff --git a/audio/out/push.c b/audio/out/push.c index c271fc0cdc..c4083923fd 100644 --- a/audio/out/push.c +++ b/audio/out/push.c @@ -423,8 +423,11 @@ static void destroy_no_thread(struct ao *ao) ao->driver->uninit(ao); - for (int n = 0; n < 2; n++) - close(p->wakeup_pipe[n]); + for (int n = 0; n < 2; n++) { + int h = p->wakeup_pipe[n]; + if (h >= 0) + close(h); + } pthread_cond_destroy(&p->wakeup); pthread_mutex_destroy(&p->lock); diff --git a/input/ipc-unix.c b/input/ipc-unix.c index f26e0cadde..c3315d21b5 100644 --- a/input/ipc-unix.c +++ b/input/ipc-unix.c @@ -402,8 +402,10 @@ struct mp_ipc_ctx *mp_init_ipc(struct mp_client_api *client_api, return arg; out: - close(arg->death_pipe[0]); - close(arg->death_pipe[1]); + if (arg->death_pipe[0] >= 0) { + close(arg->death_pipe[0]); + close(arg->death_pipe[1]); + } talloc_free(arg); return NULL; } diff --git a/osdep/subprocess-posix.c b/osdep/subprocess-posix.c index 163559edbc..c780473fce 100644 --- a/osdep/subprocess-posix.c +++ b/osdep/subprocess-posix.c @@ -32,6 +32,8 @@ extern char **environ; +#define SAFE_CLOSE(fd) do { if ((fd) >= 0) close((fd)); (fd) = -1; } while (0) + // A silly helper: automatically skips entries with negative FDs static int sparse_poll(struct pollfd *fds, int num_fds, int timeout) { @@ -93,12 +95,9 @@ int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx, } spawned = true; - close(p_stdout[1]); - p_stdout[1] = -1; - close(p_stderr[1]); - p_stderr[1] = -1; - close(devnull); - devnull = -1; + SAFE_CLOSE(p_stdout[1]); + SAFE_CLOSE(p_stderr[1]); + SAFE_CLOSE(devnull); int *read_fds[2] = {&p_stdout[0], &p_stderr[0]}; subprocess_read_cb read_cbs[2] = {on_stdout, on_stderr}; @@ -119,10 +118,8 @@ int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx, continue; if (r > 0 && read_cbs[n]) read_cbs[n](ctx, buf, r); - if (r <= 0) { - close(*read_fds[n]); - *read_fds[n] = -1; - } + if (r <= 0) + SAFE_CLOSE(*read_fds[n]); } } if (fds[2].revents) { @@ -141,11 +138,11 @@ int mp_subprocess(char **args, struct mp_cancel *cancel, void *ctx, done: if (fa_destroy) posix_spawn_file_actions_destroy(&fa); - close(p_stdout[0]); - close(p_stdout[1]); - close(p_stderr[0]); - close(p_stderr[1]); - close(devnull); + SAFE_CLOSE(p_stdout[0]); + SAFE_CLOSE(p_stdout[1]); + SAFE_CLOSE(p_stderr[0]); + SAFE_CLOSE(p_stderr[1]); + SAFE_CLOSE(devnull); if (!spawned || (WIFEXITED(status) && WEXITSTATUS(status) == 127)) { *error = "init"; 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; } diff --git a/stream/stream.c b/stream/stream.c index d4dea4486f..ca17fa787f 100644 --- a/stream/stream.c +++ b/stream/stream.c @@ -885,8 +885,10 @@ struct mp_cancel { static void cancel_destroy(void *p) { struct mp_cancel *c = p; - close(c->wakeup_pipe[0]); - close(c->wakeup_pipe[1]); + if (c->wakeup_pipe[0] >= 0) { + close(c->wakeup_pipe[0]); + close(c->wakeup_pipe[1]); + } } struct mp_cancel *mp_cancel_new(void *talloc_ctx) diff --git a/video/out/x11_common.c b/video/out/x11_common.c index 26f861be06..670dca9147 100644 --- a/video/out/x11_common.c +++ b/video/out/x11_common.c @@ -762,8 +762,10 @@ void vo_x11_uninit(struct vo *vo) sem_destroy(&x11->screensaver_sem); } - for (int n = 0; n < 2; n++) - close(x11->wakeup_pipe[n]); + if (x11->wakeup_pipe[0] >= 0) { + close(x11->wakeup_pipe[0]); + close(x11->wakeup_pipe[1]); + } talloc_free(x11); vo->x11 = NULL;