lua: workaround false positive dereference of null pointer
[project/ubus.git] / libubus-io.c
index daa710677880c1d5933b45ab9b528116c5f320ca..81c1cd1309b11d8fe905a550c3f37fcbc45667dd 100644 (file)
@@ -60,8 +60,8 @@ static void wait_data(int fd, bool write)
 static int writev_retry(int fd, struct iovec *iov, int iov_len, int sock_fd)
 {
        static struct {
-               struct cmsghdr h;
                int fd;
+               struct cmsghdr h;
        } fd_buf = {
                .h = {
                        .cmsg_len = sizeof(fd_buf),
@@ -78,7 +78,7 @@ static int writev_retry(int fd, struct iovec *iov, int iov_len, int sock_fd)
        int len = 0;
 
        do {
-               int cur_len;
+               ssize_t cur_len;
 
                if (sock_fd < 0) {
                        msghdr.msg_control = NULL;
@@ -105,7 +105,7 @@ static int writev_retry(int fd, struct iovec *iov, int iov_len, int sock_fd)
                        sock_fd = -1;
 
                len += cur_len;
-               while (cur_len >= iov->iov_len) {
+               while (cur_len >= (ssize_t) iov->iov_len) {
                        cur_len -= iov->iov_len;
                        iov_len--;
                        iov++;
@@ -159,8 +159,8 @@ static int recv_retry(struct ubus_context *ctx, struct iovec *iov, bool wait, in
        int bytes, total = 0;
        int fd = ctx->sock.fd;
        static struct {
-               struct cmsghdr h;
                int fd;
+               struct cmsghdr h;
        } fd_buf = {
                .h = {
                        .cmsg_type = SCM_RIGHTS,
@@ -174,9 +174,6 @@ static int recv_retry(struct ubus_context *ctx, struct iovec *iov, bool wait, in
        };
 
        while (iov->iov_len > 0) {
-               if (wait)
-                       wait_data(fd, false);
-
                if (recv_fd) {
                        msghdr.msg_control = &fd_buf;
                        msghdr.msg_controllen = sizeof(fd_buf);
@@ -192,8 +189,6 @@ static int recv_retry(struct ubus_context *ctx, struct iovec *iov, bool wait, in
 
                if (bytes < 0) {
                        bytes = 0;
-                       if (uloop_cancelling() || ctx->cancel_poll)
-                               return 0;
                        if (errno == EINTR)
                                continue;
 
@@ -212,12 +207,15 @@ static int recv_retry(struct ubus_context *ctx, struct iovec *iov, bool wait, in
                iov->iov_len -= bytes;
                iov->iov_base += bytes;
                total += bytes;
+
+               if (iov->iov_len > 0)
+                       wait_data(fd, false);
        }
 
        return total;
 }
 
-static bool ubus_validate_hdr(struct ubus_msghdr *hdr)
+bool ubus_validate_hdr(struct ubus_msghdr *hdr)
 {
        struct blob_attr *data = (struct blob_attr *) (hdr + 1);
 
@@ -394,7 +392,7 @@ int ubus_reconnect(struct ubus_context *ctx, const char *path)
                goto out_close;
 
        memcpy(buf, &hdr.data, sizeof(hdr.data));
-       if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != blob_len(buf))
+       if (read(ctx->sock.fd, blob_data(buf), blob_len(buf)) != (ssize_t) blob_len(buf))
                goto out_free;
 
        ctx->local_id = hdr.hdr.peer;