From dc71d91cea8543f7032492ca4a195aa521e6b07f Mon Sep 17 00:00:00 2001 From: dmiller Date: Sat, 4 Jun 2016 02:46:13 +0000 Subject: [PATCH] Avoid crashes in Windows using poll nsock engine WSAPoll returns WSAEINVAL when there are no valid sockets in the fdarray parameter. Individual WSAPOLLFDs can be ignored by setting them to a negative value (just as with POSIX poll(2)), but there must be at least one valid (not-ignored) socket to check. Handled this by either returning error if the error was not EINVAL, or by checking each WSAPOLLFD in the fdarray; at the first valid one, return the error, since this was not the reason for the error. If none are valid, continue, ignoring the error. --- nsock/src/engine_poll.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/nsock/src/engine_poll.c b/nsock/src/engine_poll.c index a0b4340c9..efc188156 100644 --- a/nsock/src/engine_poll.c +++ b/nsock/src/engine_poll.c @@ -356,9 +356,17 @@ int poll_loop(struct npool *nsp, int msec_timeout) { } while (results_left == -1 && sock_err == EINTR); /* repeat only if signal occurred */ if (results_left == -1 && sock_err != EINTR) { - nsock_log_error("nsock_loop error %d: %s", sock_err, socket_strerror(sock_err)); - nsp->errnum = sock_err; - return -1; +#ifdef WIN32 + for (int i = 0; sock_err != EINVAL || i <= pinfo->max_fd; i++) { + if (sock_err != EINVAL || pinfo->events[i].fd != -1) { +#endif + nsock_log_error("nsock_loop error %d: %s", sock_err, socket_strerror(sock_err)); + nsp->errnum = sock_err; + return -1; +#ifdef WIN32 + } + } +#endif } iterate_through_event_lists(nsp);