file: exec: properly free memory on error openwrt-18.06
authorYousong Zhou <yszhou4tech@gmail.com>
Mon, 21 Oct 2019 12:59:23 +0000 (12:59 +0000)
committerJo-Philipp Wich <jo@mein.io>
Tue, 26 May 2020 14:21:13 +0000 (16:21 +0200)
Signed-off-by: Yousong Zhou <yszhou4tech@gmail.com>
[fix whitespace]
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
(cherry picked from commit 90e40bd3d5b6d164be4c1f3583a13dc2f34d6563)

file.c

diff --git a/file.c b/file.c
index 016f264033e6f1a8181c7d53db06b84277d5281b..9c5cbbbe0ccac659b93f531c1e11a2f8c73405b5 100644 (file)
--- a/file.c
+++ b/file.c
@@ -626,13 +626,16 @@ rpc_file_exec_run(const char *cmd,
        if (!c)
                return UBUS_STATUS_UNKNOWN_ERROR;
 
-       if (pipe(opipe) || pipe(epipe))
-               return rpc_errno_status();
+       if (pipe(opipe))
+               goto fail_opipe;
+
+       if (pipe(epipe))
+               goto fail_epipe;
 
        switch ((pid = fork()))
        {
        case -1:
-               return rpc_errno_status();
+               goto fail_fork;
 
        case 0:
                uloop_done();
@@ -724,6 +727,18 @@ rpc_file_exec_run(const char *cmd,
        }
 
        return UBUS_STATUS_OK;
+
+fail_fork:
+       close(epipe[0]);
+       close(epipe[1]);
+
+fail_epipe:
+       close(opipe[0]);
+       close(opipe[1]);
+
+fail_opipe:
+       free(c);
+       return rpc_errno_status();
 }
 
 static int