]> git.itanic.dy.fi Git - rrdd/commitdiff
run_piped: Fix file descriptor leak on pipe() fail.
authorTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 25 Oct 2020 16:09:51 +0000 (18:09 +0200)
committerTimo Kokkonen <timo.t.kokkonen@iki.fi>
Sun, 25 Oct 2020 16:09:51 +0000 (18:09 +0200)
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 <timo.t.kokkonen@iki.fi>
process.c

index e2126a346d5bbde612400ac0351149cdde2e7d5d..ce46d92cc3335ac116719b8d28dee4fdf05d82a6 100644 (file)
--- 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;
 }
 
 /*