Skip to content

Commit ac020b8

Browse files
authored
wasip2: Fix poll's behavior on invalid fds (WebAssembly#680)
Skip negative fds, but fail on invalid fds.
1 parent a6158be commit ac020b8

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

libc-bottom-half/sources/poll-wasip2.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ int poll_wasip2(struct pollfd *fds, size_t nfds, int timeout)
6060

6161
for (size_t i = 0; i < nfds; ++i) {
6262
struct pollfd *pollfd = fds + i;
63+
if (pollfd->fd < 0)
64+
continue;
6365
state.pollfd = pollfd;
6466
descriptor_table_entry_t *entry = descriptor_table_get_ref(pollfd->fd);
65-
if (!entry)
66-
continue;
67+
if (!entry) {
68+
errno = EBADF;
69+
return -1;
70+
}
6771

6872
// If this descriptor has a custom registration function then
6973
// use that exclusively.

test/src/poll.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <fcntl.h>
66
#include <poll.h>
77
#include "test.h"
8+
#include <errno.h>
89

910
#define TEST(c, ...) ((c) ? 1 : (t_error(#c" failed: " __VA_ARGS__),0))
1011

@@ -32,5 +33,15 @@ int main(void)
3233
if (fd > 2)
3334
TEST(unlink(tmp) != -1);
3435

36+
// skip negative fds
37+
poll_fd.fd = -300;
38+
r = poll(&poll_fd, 1, 0);
39+
TEST(r==0, "poll returned %d, expected 0\n", r);
40+
41+
// fail on invalid fds
42+
poll_fd.fd = 300;
43+
r = poll(&poll_fd, 1, 0);
44+
int err = errno;
45+
TEST(r==-1 && err==EBADF, "poll returned %d, expected -1\n", r);
3546
return t_status;
3647
}

0 commit comments

Comments
 (0)