iov_send_recv(): Handle zero bytes case even if OS does not
authorPeter Maydell <peter.maydell@linaro.org>
Sat, 11 Aug 2012 21:24:35 +0000 (22:24 +0100)
committerStefan Hajnoczi <stefanha@linux.vnet.ibm.com>
Wed, 15 Aug 2012 14:21:33 +0000 (15:21 +0100)
POSIX allows sendmsg() and recvmsg() to fail EMSGSIZE if passed a zero
msg.msg_iovlen (in particular the MacOS X implementation will do this).
Handle the case where iov_send_recv() is passed a zero byte count
explicitly, to avoid accidentally depending on the OS to treat zero
msg_iovlen as a no-op.

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Acked-by: Michael Tokarev <mjt@tls.msk.ru>
Signed-off-by: Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
iov.c

diff --git a/iov.c b/iov.c
index b3330610bba4fb1e72f3f87266e6fa8bec6da19a..60705c73ab118d390383d2b94b2a806b37927fee 100644 (file)
--- a/iov.c
+++ b/iov.c
@@ -146,6 +146,13 @@ ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
 {
     ssize_t ret;
     unsigned si, ei;            /* start and end indexes */
+    if (bytes == 0) {
+        /* Catch the do-nothing case early, as otherwise we will pass an
+         * empty iovec to sendmsg/recvmsg(), and not all implementations
+         * accept this.
+         */
+        return 0;
+    }
 
     /* Find the start position, skipping `offset' bytes:
      * first, skip all full-sized vector elements, */