Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions libc-bottom-half/sources/file_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ ssize_t __wasilibc_write(wasi_write_t *write, const void *buffer,
assert((state->flags & WASIP3_IO_INPROGRESS) == 0);
assert((state->flags & WASIP3_IO_ZERO_INPROGRESS) == 0);

// Follow posix semantics for zero-length writes which appear to return 0
// without actually doing any I/O.
if (length == 0)
return 0;

// If this stream is complete, then delegate to EOF.
if (state->flags & WASIP3_IO_DONE)
return write->eof(write->eof_data);
Expand Down Expand Up @@ -587,6 +592,11 @@ ssize_t __wasilibc_read(wasi_read_t *read, void *buffer, size_t length) {
if (state->buf)
return wasip3_read_complete_internally(state, buffer, length);

// Implement posix-like semantics where a `read` of 0 bytes doesn't do
// anything and just returns 0.
if (length == 0)
return 0;

// If this stream has finished, then delegate to EOF.
if (state->flags & WASIP3_IO_DONE)
return read->eof(read->eof_data);
Expand Down
5 changes: 4 additions & 1 deletion test/src/sockets-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ void test_tcp_client(int server_port) {
TEST(bytes_received == len);

// Message received should be the same as message sent
TEST(strcmp(message, client_buffer) == 0);
TEST(memcmp(message, client_buffer, len) == 0);

TEST(read(socket_fd, NULL, 0) == 0);
TEST(write(socket_fd, NULL, 0) == 0);

// Shut down client
close(socket_fd);
Expand Down
5 changes: 5 additions & 0 deletions test/src/sockets-nonblocking.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ void test_tcp_client() {
TEST(poll(&poll_fd, 1, -1) != -1);
}

TEST(read(client_socket_fd, NULL, 0) == 0);
TEST(write(client_socket_fd, NULL, 0) == 0);
TEST(read(socket_fd, NULL, 0) == 0);
TEST(write(socket_fd, NULL, 0) == 0);

// Message received should be the same as message sent
TEST(strcmp(message, client_buffer) == 0);

Expand Down