From ceaf7dc660c43fc3a7a851cd0d92d4c8d5e72742 Mon Sep 17 00:00:00 2001 From: "liang.he" Date: Tue, 10 May 2022 10:43:34 +0800 Subject: [PATCH] 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. --- .../libraries/lib-socket/src/wasi/wasi_socket_ext.c | 4 ++-- core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c | 4 ++-- samples/socket-api/wasm-src/send_recv.c | 11 ++++++----- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/core/iwasm/libraries/lib-socket/src/wasi/wasi_socket_ext.c b/core/iwasm/libraries/lib-socket/src/wasi/wasi_socket_ext.c index f5236fcf6..a96156f7e 100644 --- a/core/iwasm/libraries/lib-socket/src/wasi/wasi_socket_ext.c +++ b/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) diff --git a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c b/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c index 7b4a82898..7611999ad 100644 --- a/core/iwasm/libraries/libc-wasi/libc_wasi_wrapper.c +++ b/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 diff --git a/samples/socket-api/wasm-src/send_recv.c b/samples/socket-api/wasm-src/send_recv.c index 9ac0aad31..228fd598c 100644 --- a/samples/socket-api/wasm-src/send_recv.c +++ b/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 +#include #include #include #include @@ -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; }