Instead of using strlen() to calculate length of payload we're
sending, let caller specify the size: they may want to send just
a portion of a buffer (even though the only current user
doesn't).
Signed-off-by: Michal Privoznik <mprivozn(a)redhat.com>
---
src/ch/ch_process.c | 5 ++++-
src/util/virsocket.c | 10 ++++++++--
src/util/virsocket.h | 4 ++--
3 files changed, 14 insertions(+), 5 deletions(-)
diff --git a/src/ch/ch_process.c b/src/ch/ch_process.c
index 3265ae90de..3bde9d9dcf 100644
--- a/src/ch/ch_process.c
+++ b/src/ch/ch_process.c
@@ -558,6 +558,7 @@ chProcessAddNetworkDevices(virCHDriver *driver,
g_autofree char *response = NULL;
size_t j;
size_t tapfd_len;
+ size_t payload_len;
int saved_errno;
int http_res;
int rc;
@@ -595,9 +596,11 @@ chProcessAddNetworkDevices(virCHDriver *driver,
virBufferAsprintf(&buf, "%s",
virBufferCurrentContent(&http_headers));
virBufferAsprintf(&buf, "Content-Length: %ld\r\n\r\n",
strlen(payload));
virBufferAsprintf(&buf, "%s", payload);
+ payload_len = virBufferUse(&buf);
payload = virBufferContentAndReset(&buf);
- rc = virSocketSendMsgWithFDs(mon_sockfd, payload, tapfds, tapfd_len);
+ rc = virSocketSendMsgWithFDs(mon_sockfd, payload, payload_len,
+ tapfds, tapfd_len);
saved_errno = errno;
/* Close sent tap fds in Libvirt, as they have been dup()ed in CH */
diff --git a/src/util/virsocket.c b/src/util/virsocket.c
index ff06eb15f7..a7272a3ec9 100644
--- a/src/util/virsocket.c
+++ b/src/util/virsocket.c
@@ -488,6 +488,7 @@ virSocketRecvFD(int sock, int fdflags)
* virSocketSendMsgWithFDs:
* @sock: socket to send payload and fds to
* @payload: payload to send
+ * @payload_len: length of @payload
* @fds: array of fds to send
* @fds_len: len of fds array
@@ -496,7 +497,11 @@ virSocketRecvFD(int sock, int fdflags)
* On error, set errno and return -1.
*/
int
-virSocketSendMsgWithFDs(int sock, const char *payload, int *fds, size_t fds_len)
+virSocketSendMsgWithFDs(int sock,
+ const char *payload,
+ size_t payload_len,
+ int *fds,
+ size_t fds_len)
{
g_autofree char *control = NULL;
const size_t control_size = CMSG_SPACE(sizeof(int) * fds_len);
@@ -508,7 +513,7 @@ virSocketSendMsgWithFDs(int sock, const char *payload, int *fds,
size_t fds_len)
control = g_new0(char, control_size);
iov[0].iov_base = (void *) payload;
- iov[0].iov_len = strlen(payload);
+ iov[0].iov_len = payload_len;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
@@ -553,6 +558,7 @@ virSocketRecvFD(int sock G_GNUC_UNUSED, int fdflags G_GNUC_UNUSED)
int
virSocketSendMsgWithFDs(int sock G_GNUC_UNUSED,
const char *payload G_GNUC_UNUSED,
+ size_t payload_len G_GNUC_UNUSED,
int *fds G_GNUC_UNUSED,
size_t fds_len G_GNUC_UNUSED)
{
diff --git a/src/util/virsocket.h b/src/util/virsocket.h
index 31a31c2378..0e7f9616f2 100644
--- a/src/util/virsocket.h
+++ b/src/util/virsocket.h
@@ -22,8 +22,8 @@
int virSocketSendFD(int sock, int fd);
int virSocketRecvFD(int sock, int fdflags);
-int virSocketSendMsgWithFDs(int sock, const char *payload, int *fds,
- size_t fd_len);
+int virSocketSendMsgWithFDs(int sock, const char *payload, size_t payload_len,
+ int *fds, size_t fds_len);
#ifdef WIN32
--
2.43.0