lib: Simplify iov_buf[len]
authorVolker Lendecke <vl@samba.org>
Sat, 6 Dec 2014 10:22:35 +0000 (11:22 +0100)
committerJeremy Allison <jra@samba.org>
Tue, 9 Dec 2014 03:12:08 +0000 (04:12 +0100)
This makes iov_buf independent of talloc

Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
source3/lib/iov_buf.c
source3/lib/iov_buf.h
source3/lib/messages_ctdbd.c
source3/wscript_build

index dd99da3625eb859d243cf92c6bbd55343653963f..e05dfc9524895bd537cfdf004dce5cda599019f9 100644 (file)
 
 ssize_t iov_buflen(const struct iovec *iov, int iovcnt)
 {
-       size_t buflen = 0;
+       return iov_buf(iov, iovcnt, NULL, 0);
+}
+
+ssize_t iov_buf(const struct iovec *iov, int iovcnt,
+               uint8_t *buf, size_t buflen)
+{
+       size_t needed = 0;
+       uint8_t *p = buf;
        int i;
 
        for (i=0; i<iovcnt; i++) {
                size_t thislen = iov[i].iov_len;
-               size_t tmp = buflen + thislen;
+               size_t tmp;
+
+               tmp = needed + thislen;
 
-               if ((tmp < buflen) || (tmp < thislen)) {
+               if ((tmp < needed) || (tmp < thislen)) {
                        /* overflow */
                        return -1;
                }
-               buflen = tmp;
-       }
-       return buflen;
-}
-
-uint8_t *iov_buf(TALLOC_CTX *mem_ctx, const struct iovec *iov, int iovcnt)
-{
-       int i;
-       ssize_t buflen;
-       uint8_t *buf, *p;
+               needed = tmp;
 
-       buflen = iov_buflen(iov, iovcnt);
-       if (buflen == -1) {
-               return NULL;
-       }
-       buf = talloc_array(mem_ctx, uint8_t, buflen);
-       if (buf == NULL) {
-               return NULL;
+               if (needed <= buflen) {
+                       memcpy(p, iov[i].iov_base, thislen);
+                       p += thislen;
+               }
        }
 
-       p = buf;
-       for (i=0; i<iovcnt; i++) {
-               size_t len = iov[i].iov_len;
-
-               memcpy(p, iov[i].iov_base, len);
-               p += len;
-       }
-       return buf;
+       return needed;
 }
index a884bdbe0ef44c8a89ebfed6091438c93a37ecc3..ec8290989ac3e712ed2fc70efab7e1d03191cf14 100644 (file)
 
 #include <unistd.h>
 #include <talloc.h>
+#include <stdint.h>
 
 ssize_t iov_buflen(const struct iovec *iov, int iovlen);
-uint8_t *iov_buf(TALLOC_CTX *mem_ctx, const struct iovec *iov, int iovcnt);
+ssize_t iov_buf(const struct iovec *iov, int iovcnt,
+               uint8_t *buf, size_t buflen);
 
 #endif
index 59f5976da0b8a397f8f537887192d7955e250070..53aeb1fd057ab62563d09be46a30658670e53178 100644 (file)
@@ -100,16 +100,23 @@ static int messaging_ctdb_send(struct server_id src,
                backend->private_data, struct messaging_ctdbd_context);
        struct messaging_rec msg;
        uint8_t *buf;
+       ssize_t buflen;
        NTSTATUS status;
 
        if (num_fds > 0) {
                return ENOSYS;
        }
 
-       buf = iov_buf(talloc_tos(), iov, iovlen);
-       if (buf == NULL) {
+       buflen = iov_buflen(iov, iovlen);
+       if (buflen == -1) {
+               return EMSGSIZE;
+       }
+
+       buf = talloc_array(talloc_tos(), uint8_t, buflen);
+       if (buflen == NULL) {
                return ENOMEM;
        }
+       iov_buf(iov, iovlen, buf, buflen);
 
        msg = (struct messaging_rec) {
                .msg_version    = MESSAGE_VERSION,
index 18f6b6db7c58065c552dcd30117e6292a3b7813c..51d72e78b640981ebc60a5a450ae57c089bef6f4 100755 (executable)
@@ -260,7 +260,7 @@ bld.SAMBA3_LIBRARY('sys_rw',
 
 bld.SAMBA3_LIBRARY('iov_buf',
                    source='lib/iov_buf.c',
-                   deps='replace talloc',
+                   deps='replace',
                    private_library=True)
 
 bld.SAMBA3_SUBSYSTEM('samba3util',