sys: Check return values of chdir and write
authorFlorian Fainelli <f.fainelli@gmail.com>
Sat, 3 Dec 2016 17:34:45 +0000 (09:34 -0800)
committerJohn Crispin <john@phrozen.org>
Sun, 11 Dec 2016 08:15:27 +0000 (09:15 +0100)
Fixes the following warnings:

rpcd/sys.c: In function 'rpc_cgi_password_set':
rpcd/sys.c:116:8: error: ignoring return value of 'chdir', declared with attribute warn_unused_result [-Werror=unused-result]
   chdir("/");
        ^
rpcd/sys.c:125:8: error: ignoring return value of 'write', declared with attribute warn_unused_result [-Werror=unused-result]
   write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
        ^
rpcd/sys.c:127:8: error: ignoring return value of 'write', declared with attribute warn_unused_result [-Werror=unused-result]
   write(fds[1], "\n", 1);
        ^
rpcd/sys.c:131:8: error: ignoring return value of 'write', declared with attribute warn_unused_result [-Werror=unused-result]
   write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
        ^
rpcd/sys.c:133:8: error: ignoring return value of 'write', declared with attribute warn_unused_result [-Werror=unused-result]
   write(fds[1], "\n", 1);
        ^
cc1: all warnings being treated as errors

Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
sys.c

diff --git a/sys.c b/sys.c
index b90aa6b894aba29e8be4884c160a7c34ee0ca646..3691dfbd1f50aac3f5bb33764c07ffb62c1c743c 100644 (file)
--- a/sys.c
+++ b/sys.c
@@ -76,6 +76,8 @@ rpc_cgi_password_set(struct ubus_context *ctx, struct ubus_object *obj,
        int fd, fds[2];
        struct stat s;
        struct blob_attr *tb[__RPC_P_MAX];
+       ssize_t n;
+       int ret;
 
        blobmsg_parse(rpc_password_policy, __RPC_P_MAX, tb,
                      blob_data(msg), blob_len(msg));
@@ -113,7 +115,9 @@ rpc_cgi_password_set(struct ubus_context *ctx, struct ubus_object *obj,
                        close(fd);
                }
 
-               chdir("/");
+               ret = chdir("/");
+               if (ret < 0)
+                       return rpc_errno_status();
 
                if (execl("/usr/bin/passwd", "/usr/bin/passwd",
                          blobmsg_data(tb[RPC_P_USER]), NULL))
@@ -122,15 +126,24 @@ rpc_cgi_password_set(struct ubus_context *ctx, struct ubus_object *obj,
        default:
                close(fds[0]);
 
-               write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
+               n = write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
                              blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
-               write(fds[1], "\n", 1);
+               if (n < 0)
+                       return rpc_errno_status();
+
+               n = write(fds[1], "\n", 1);
+               if (n < 0)
+                       return rpc_errno_status();
 
                usleep(100 * 1000);
 
-               write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
+               n = write(fds[1], blobmsg_data(tb[RPC_P_PASSWORD]),
                              blobmsg_data_len(tb[RPC_P_PASSWORD]) - 1);
-               write(fds[1], "\n", 1);
+               if (n < 0)
+                       return rpc_errno_status();
+               n = write(fds[1], "\n", 1);
+               if (n < 0)
+                       return rpc_errno_status();
 
                close(fds[1]);