Copy only received bytes from socket recv buffer into the app buffer (#1497)

**What**

* Updated `copy_buffer_to_iovec_app` so that it copies as much of the buffer into the iovec as specified
* Throw invalid value when allocating an iovec of size 0

**Why**

* A bug found from TCP client example which allocates 1024 for the iovec size (where the buf size is also 1024) but received bytes is passed in as the `buf_size` argument to `copy_buffer_to_iovec_app`. This would return early after hitting this check `buf + data->buf_len > buf_begin + buf_size`. However, if the amount to copy is less than the iovec size, we should copy that much of the buf size. Eg TCP client sample receives 27(?) bytes at a time, and this copies 27 bytes into the iovec of size 1024
* The TCP client example attempts to recv bytes of size 0, this attempts to wasm malloc size 0, which outputs a warning. We should early return if recv bytes of size 0
This commit is contained in:
Callum Macmillan
2022-09-20 23:11:03 +01:00
committed by GitHub
parent c072b5172c
commit 8dd1c8ab86
2 changed files with 35 additions and 6 deletions

View File

@ -91,6 +91,8 @@ ExternalProject_Add(wasm-app
tcp_server.wasm ${CMAKE_BINARY_DIR}
send_recv.wasm ${CMAKE_BINARY_DIR}
socket_opts.wasm ${CMAKE_BINARY_DIR}
udp_client.wasm ${CMAKE_BINARY_DIR}
udp_server.wasm ${CMAKE_BINARY_DIR}
)
add_executable(tcp_server ${CMAKE_CURRENT_SOURCE_DIR}/wasm-src/tcp_server.c)