This commit is contained in:
Jordan Woyak 2026-03-20 23:40:55 -05:00 committed by GitHub
commit bdc24f8a77
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 36 additions and 13 deletions

View File

@ -10,8 +10,13 @@
#include <type_traits>
#include <vector>
#if defined(__linux__) || defined(__HAIKU__)
#if defined(_WIN32)
#include <winsock2.h>
#else
#include <sys/select.h>
#include <sys/socket.h>
#include "Common/Logging/Log.h"
#endif
#include "Common/CommonTypes.h"
@ -318,4 +323,20 @@ static constexpr int SEND_FLAGS = MSG_NOSIGNAL;
static constexpr int SEND_FLAGS = 0;
#endif
// TODO: Don't use FD_SET and select().
// See WARNING at https://www.man7.org/linux/man-pages/man2/select.2.html
constexpr void Safe_FD_SET(auto fd, fd_set* fds)
{
#if !defined(_WIN32)
// On non-Windows, fd_set is a bitset and socket values must be within [0, FD_SETSIZE).
if (fd < 0 || fd >= FD_SETSIZE)
{
ERROR_LOG_FMT(COMMON, "FD_SET: Invalid socket: {}", fd);
return;
}
#endif
FD_SET(fd, fds);
}
} // namespace Common

View File

@ -20,6 +20,7 @@
#include <systemd/sd-daemon.h>
#endif
#include "Common/Network.h"
#include "Common/Random.h"
#include "Common/TraversalProto.h"
@ -467,8 +468,8 @@ int main()
tv.tv_usec = 300000;
fd_set readSet;
FD_ZERO(&readSet);
FD_SET(sock, &readSet);
FD_SET(sockAlt, &readSet);
Common::Safe_FD_SET(sock, &readSet);
Common::Safe_FD_SET(sockAlt, &readSet);
rv = select(std::max(sock, sockAlt) + 1, &readSet, nullptr, nullptr, &tv);
if (rv < 0)
{

View File

@ -844,8 +844,8 @@ BbaTcpSocket::ConnectingState BbaTcpSocket::Connected(StackRef* ref)
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
FD_ZERO(&except_fds);
FD_SET(fd, &write_fds);
FD_SET(fd, &except_fds);
Common::Safe_FD_SET(fd, &write_fds);
Common::Safe_FD_SET(fd, &except_fds);
if (select(nfds, &read_fds, &write_fds, &except_fds, &t) < 0)
{

View File

@ -249,7 +249,7 @@ void TAPServerConnection::ReadThreadHandler()
{
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(m_fd, &rfds);
Common::Safe_FD_SET(m_fd, &rfds);
timeval timeout;
timeout.tv_sec = 0;

View File

@ -129,7 +129,7 @@ void CEXIETHERNET::TAPNetworkInterface::ReadThreadHandler(TAPNetworkInterface* s
{
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(self->fd, &rfds);
Common::Safe_FD_SET(self->fd, &rfds);
struct timeval timeout;
timeout.tv_sec = 0;

View File

@ -773,8 +773,8 @@ WiiSocket::ConnectingState WiiSocket::GetConnectingState() const
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
FD_ZERO(&except_fds);
FD_SET(fd, &write_fds);
FD_SET(fd, &except_fds);
Common::Safe_FD_SET(fd, &write_fds);
Common::Safe_FD_SET(fd, &except_fds);
if (select(nfds, &read_fds, &write_fds, &except_fds, &t) < 0)
{
@ -1007,9 +1007,9 @@ void WiiSockMan::Update()
const WiiSocket& sock = socket_iter->second;
if (sock.IsValid())
{
FD_SET(sock.fd, &read_fds);
FD_SET(sock.fd, &write_fds);
FD_SET(sock.fd, &except_fds);
Common::Safe_FD_SET(sock.fd, &read_fds);
Common::Safe_FD_SET(sock.fd, &write_fds);
Common::Safe_FD_SET(sock.fd, &except_fds);
nfds = std::max(nfds, sock.fd + 1);
++socket_iter;
}

View File

@ -24,6 +24,7 @@ typedef SSIZE_T ssize_t;
#include "Common/Assert.h"
#include "Common/Logging/Log.h"
#include "Common/Network.h"
#include "Common/SocketContext.h"
#include "Core/Core.h"
#include "Core/HW/CPU.h"
@ -260,7 +261,7 @@ static bool IsDataAvailable()
fd_set _fds, *fds = &_fds;
FD_ZERO(fds);
FD_SET(s_sock, fds);
Common::Safe_FD_SET(s_sock, fds);
t.tv_sec = 0;
t.tv_usec = 20;