From 36057b93f3a248b7cf7599185683f008fce18546 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Fri, 5 Sep 2025 23:20:45 +0100 Subject: [PATCH] Fix GH-19722: Windows: _get_osfhandle asserts in debug mode when given a socket Closes GH-19725. --- NEWS | 4 ++++ win32/select.c | 17 +++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/NEWS b/NEWS index b230ae812e0..484ce8e2c63 100644 --- a/NEWS +++ b/NEWS @@ -11,6 +11,10 @@ PHP NEWS . Fixed bug GH-19780 (InvalidUrlException should check $errors argument). (nielsdos) +- Windows: + . Fix GH-19722 (_get_osfhandle asserts in debug mode when given a socket). + (dktapps) + 11 Sep 2025, PHP 8.5.0beta3 - Core: diff --git a/win32/select.c b/win32/select.c index dec149b665f..0cd7b852955 100644 --- a/win32/select.c +++ b/win32/select.c @@ -66,8 +66,10 @@ PHPAPI int php_select(php_socket_t max_fd, fd_set *rfds, fd_set *wfds, fd_set *e /* build an array of handles for non-sockets */ for (i = 0; (uint32_t)i < max_fd; i++) { if (SAFE_FD_ISSET(i, rfds) || SAFE_FD_ISSET(i, wfds) || SAFE_FD_ISSET(i, efds)) { - handles[n_handles] = (HANDLE)(uintptr_t)_get_osfhandle(i); - if (handles[n_handles] == INVALID_HANDLE_VALUE) { + int _type; + int _len = sizeof(_type); + + if (getsockopt((SOCKET)i, SOL_SOCKET, SO_TYPE, (char*)&_type, &_len) == 0 || WSAGetLastError() != WSAENOTSOCK) { /* socket */ if (SAFE_FD_ISSET(i, rfds)) { FD_SET((uint32_t)i, &sock_read); @@ -82,11 +84,14 @@ PHPAPI int php_select(php_socket_t max_fd, fd_set *rfds, fd_set *wfds, fd_set *e sock_max_fd = i; } } else { - if (SAFE_FD_ISSET(i, rfds) && GetFileType(handles[n_handles]) == FILE_TYPE_PIPE) { - num_read_pipes++; + handles[n_handles] = (HANDLE)(uintptr_t)_get_osfhandle(i); + if (handles[n_handles] != INVALID_HANDLE_VALUE) { + if (SAFE_FD_ISSET(i, rfds) && GetFileType(handles[n_handles]) == FILE_TYPE_PIPE) { + num_read_pipes++; + } + handle_slot_to_fd[n_handles] = i; + n_handles++; } - handle_slot_to_fd[n_handles] = i; - n_handles++; } } }