From 160e2343bd7ab4f3b5f2bd8c89535709cc4bf519 Mon Sep 17 00:00:00 2001 From: Marek Vrbka Date: Wed, 13 Aug 2025 15:36:21 +0200 Subject: [PATCH] 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 Reviewed-on: https://review.openocd.org/c/openocd/+/9081 Tested-by: jenkins Reviewed-by: Antonio Borneo --- src/helper/replacements.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/helper/replacements.c b/src/helper/replacements.c index 782d97518..9fb7b4a35 100644 --- a/src/helper/replacements.c +++ b/src/helper/replacements.c @@ -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)) {