summaryrefslogtreecommitdiffstats
path: root/source3/lib
diff options
context:
space:
mode:
authorVolker Lendecke <vl@samba.org>2014-12-06 11:22:35 +0100
committerJeremy Allison <jra@samba.org>2014-12-09 04:12:08 +0100
commitd775c386e498d4c2062f8fc65f515f991d127dc1 (patch)
tree46bc6a9e8ec84b31d7a6f13138d8a6715a141423 /source3/lib
parent3a6a6f19410606a9028861ca95ac80d2651e2830 (diff)
downloadsamba-d775c386e498d4c2062f8fc65f515f991d127dc1.tar.gz
samba-d775c386e498d4c2062f8fc65f515f991d127dc1.tar.xz
samba-d775c386e498d4c2062f8fc65f515f991d127dc1.zip
lib: Simplify iov_buf[len]
This makes iov_buf independent of talloc Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
Diffstat (limited to 'source3/lib')
-rw-r--r--source3/lib/iov_buf.c46
-rw-r--r--source3/lib/iov_buf.h4
-rw-r--r--source3/lib/messages_ctdbd.c11
3 files changed, 30 insertions, 31 deletions
diff --git a/source3/lib/iov_buf.c b/source3/lib/iov_buf.c
index dd99da3625..e05dfc9524 100644
--- a/source3/lib/iov_buf.c
+++ b/source3/lib/iov_buf.c
@@ -23,43 +23,33 @@
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;
}
diff --git a/source3/lib/iov_buf.h b/source3/lib/iov_buf.h
index a884bdbe0e..ec8290989a 100644
--- a/source3/lib/iov_buf.h
+++ b/source3/lib/iov_buf.h
@@ -22,8 +22,10 @@
#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
diff --git a/source3/lib/messages_ctdbd.c b/source3/lib/messages_ctdbd.c
index 59f5976da0..53aeb1fd05 100644
--- a/source3/lib/messages_ctdbd.c
+++ b/source3/lib/messages_ctdbd.c
@@ -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,