From 4816f9e02dd47a343fbac59f2955259c6c2dffdd Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Sun, 25 Oct 2020 18:09:51 +0200 Subject: [PATCH] run_piped: Fix file descriptor leak on pipe() fail. If pipe() call fails for any reason, the already opened descriptors need to be all closed. These typically fail if we are already leaking descriptors elsewhere and we can't create any more new descriptors. Signed-off-by: Timo Kokkonen --- process.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/process.c b/process.c index e2126a3..ce46d92 100644 --- a/process.c +++ b/process.c @@ -422,12 +422,12 @@ int run_piped(const char *cmd, char *const argv[], if (stdoutfd && pipe(ofd)) { pr_err("pipe() failed: %m\n"); - return -1; + goto close_ifd; } if (stderrfd && pipe(efd)) { pr_err("pipe() failed: %m\n"); - return -1; + goto close_ofd; } pid = do_fork(); @@ -471,6 +471,19 @@ int run_piped(const char *cmd, char *const argv[], exit(1); return 0; + +close_ofd: + if (stdoutfd) { + close(ofd[0]); + close(ofd[1]); + } +close_ifd: + if (stdinfd) { + close(ifd[0]); + close(ifd[1]); + } + + return -1; } /* -- 2.45.0