replacements: make socket_select() work on Windows if no sockets are provided

On Windows 11, if select() is called with empty sets, it fails and
returns WSAINVAL. On POSIX this works fine.

This patch addresses it by detecting this case in OpenOCD replacements
and returning 0 in these cases.

This fixes OpenOCD crash on Windows if no services are enabled
(gdb server, tcl server and telnet server all disabled).

Change-Id: I601878671caf4ae44e105d6a819251d2d96c607c
Signed-off-by: Marek Vrbka <marek.vrbka@codasip.com>
Reviewed-on: https://review.openocd.org/c/openocd/+/9081
Tested-by: jenkins
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
This commit is contained in:
Marek Vrbka
2025-08-13 15:36:21 +02:00
committed by Antonio Borneo
parent 7e403d9d3b
commit 160e2343bd

View File

@@ -152,6 +152,21 @@ int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct time
FD_ZERO(&sock_write);
FD_ZERO(&sock_except);
/* On Windows, if all provided sets are empty/NULL an error code of -1 is returned
* and WSAGetLastError() returns WSAEINVAL instead of delaying.
* We check for this case, delay and return 0 instead of calling select(). */
if (rfds && rfds->fd_count == 0)
rfds = NULL;
if (wfds && wfds->fd_count == 0)
wfds = NULL;
if (efds && efds->fd_count == 0)
efds = NULL;
if (!rfds && !wfds && !efds && tv) {
sleep(tv->tv_sec);
usleep(tv->tv_usec);
return 0;
}
/* build an array of handles for non-sockets */
for (i = 0; i < max_fd; i++) {
if (SAFE_FD_ISSET(i, rfds) || SAFE_FD_ISSET(i, wfds) || SAFE_FD_ISSET(i, efds)) {