From: Timo Kokkonen Date: Sun, 25 Oct 2020 16:09:51 +0000 (+0200) Subject: run_piped: Fix file descriptor leak on pipe() fail. X-Git-Url: http://git.itanic.dy.fi/?p=rrdd;a=commitdiff_plain;h=4816f9e02dd47a343fbac59f2955259c6c2dffdd 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 --- 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; } /*