Просмотр исходного кода

Fix invalid calculation of total size of bytes to send and recv (#1162)

The total size of bytes to send and recv should be set to 0 before calculation,
but not including the size of iovec_app_t structure.
liang.he 3 лет назад
Родитель
Сommit
ceaf7dc660

+ 2 - 2
core/iwasm/libraries/lib-socket/src/wasi/wasi_socket_ext.c

@@ -154,8 +154,8 @@ recvmsg(int sockfd, struct msghdr *msg, int flags)
     // Prepare input parameters.
     __wasi_iovec_t *ri_data = NULL;
     size_t i = 0;
-    size_t ro_datalen;
-    __wasi_roflags_t ro_flags;
+    size_t ro_datalen = 0;
+    __wasi_roflags_t ro_flags = 0;
 
     if (NULL == msg) {
         HANDLE_ERROR(__WASI_ERRNO_INVAL)

+ 2 - 2
core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c

@@ -1207,7 +1207,7 @@ wasi_sock_recv(wasm_exec_env_t exec_env, wasi_fd_t sock, iovec_app_t *ri_data,
         return __WASI_EINVAL;
 
     /* receive and scatter*/
-    for (i = 0; i < ri_data_len; i++, ri_data++) {
+    for (total_size = 0, i = 0; i < ri_data_len; i++, ri_data++) {
         total_size += ri_data->buf_len;
     }
     if (total_size >= UINT32_MAX
@@ -1286,7 +1286,7 @@ wasi_sock_send(wasm_exec_env_t exec_env, wasi_fd_t sock,
         return __WASI_EINVAL;
 
     /* gather and send */
-    for (i = 0; i < si_data_len; i++, si_data++) {
+    for (total_size = 0, i = 0; i < si_data_len; i++, si_data++) {
         total_size += si_data->buf_len;
     }
     if (total_size >= UINT32_MAX

+ 6 - 5
samples/socket-api/wasm-src/send_recv.c

@@ -2,8 +2,8 @@
  * Copyright (C) 2019 Intel Corporation.  All rights reserved.
  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  */
-
 #include <arpa/inet.h>
+#include <assert.h>
 #include <netinet/in.h>
 #include <pthread.h>
 #include <stdbool.h>
@@ -109,7 +109,8 @@ run_as_client(void *arg)
 {
     int sock = -1;
     struct sockaddr_in addr = { 0 };
-    char buf[256] = { 0 };
+    /* buf of server is 106 bytes */
+    char buf[110] = { 0 };
     struct iovec iov = { .iov_base = buf, .iov_len = sizeof(buf) };
     struct msghdr msg = { .msg_iov = &iov, .msg_iovlen = 1 };
     ssize_t recv_len = 0;
@@ -145,11 +146,11 @@ run_as_client(void *arg)
     }
 
     printf("Receive %ld bytes successlly!\n", recv_len);
-    printf("Data:\n");
+    assert(recv_len == 106);
 
-    uint8_t i = 0;
+    printf("Data:\n");
     char *s = msg.msg_iov->iov_base;
-    for (i = 0; i < 6; i++) {
+    while (strlen(s) > 0) {
         printf("  %s\n", s);
         s += strlen(s) + 1;
     }