[Spice-devel] [PATCH spice-server 04/23] websocket: Simplify and fix constrain_iov

Frediano Ziglio fziglio at redhat.com
Tue Jun 25 16:11:28 UTC 2019


Use g_memdup instead of manual copy.
Trim the original iov if necessary.

Signed-off-by: Frediano Ziglio <fziglio at redhat.com>
---
 server/websocket.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)

diff --git a/server/websocket.c b/server/websocket.c
index 58f36da0a..fc1d82c28 100644
--- a/server/websocket.c
+++ b/server/websocket.c
@@ -310,26 +310,24 @@ static void constrain_iov(struct iovec *iov, int iovcnt,
                           struct iovec **iov_out, int *iov_out_cnt,
                           uint64_t maxlen)
 {
-    int i, j;
-
-    *iov_out = iov;
-    *iov_out_cnt = iovcnt;
+    int i;
 
     for (i = 0; i < iovcnt && maxlen > 0; i++) {
         if (iov[i].iov_len > maxlen) {
             /* TODO - This code has never triggered afaik... */
-            *iov_out_cnt = i + 1;
-            *iov_out = g_malloc((*iov_out_cnt) * sizeof (**iov_out));
-            for (j = 0; j < i; j++) {
-                (*iov_out)[j].iov_base = iov[j].iov_base;
-                (*iov_out)[j].iov_len = iov[j].iov_len;
-            }
-            (*iov_out)[j].iov_base = iov[j].iov_base;
-            (*iov_out)[j].iov_len = maxlen;
-            break;
+            *iov_out_cnt = ++i;
+            *iov_out = g_memdup(iov, i * sizeof (*iov));
+            (*iov_out)[i-1].iov_len = maxlen;
+            return;
         }
         maxlen -= iov[i].iov_len;
     }
+
+    /* we must trim the iov in case maxlen initially matches some chunks
+     * For instance if initially we had 2 chunks 256 and 128 bytes respectively
+     * and a maxlen of 256 we should just return the first chunk */
+    *iov_out_cnt = i;
+    *iov_out = iov;
 }
 
 
-- 
2.20.1



More information about the Spice-devel mailing list