From 0a36069315b0f6ae7cabb4d6a6dad5dff0f7944c Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Fri, 5 May 2023 02:00:03 -0700 Subject: [PATCH] Util: Fix handling of SocketPoll of n > 1 --- include/mgba-util/socket.h | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/include/mgba-util/socket.h b/include/mgba-util/socket.h index 96edc3d92..ff9d89626 100644 --- a/include/mgba-util/socket.h +++ b/include/mgba-util/socket.h @@ -434,9 +434,9 @@ static inline int SocketPoll(size_t nSockets, Socket* reads, Socket* writes, Soc #else int result = select(maxFd, &rset, &wset, &eset, timeoutMillis < 0 ? 0 : &tv); #endif - int r = 0; - int w = 0; - int e = 0; + size_t r = 0; + size_t w = 0; + size_t e = 0; Socket j; for (j = 0; j < maxFd; ++j) { if (reads && FD_ISSET(j, &rset)) { @@ -452,6 +452,21 @@ static inline int SocketPoll(size_t nSockets, Socket* reads, Socket* writes, Soc ++e; } } + if (reads) { + for (; r < nSockets; ++r) { + reads[r] = INVALID_SOCKET; + } + } + if (writes) { + for (; w < nSockets; ++w) { + writes[w] = INVALID_SOCKET; + } + } + if (errors) { + for (; e < nSockets; ++e) { + errors[e] = INVALID_SOCKET; + } + } return result; }