Skip to content

Commit

Permalink
[mdns] Fix select issue on FreeBSD.
Browse files Browse the repository at this point in the history
As reported in issue #1654, using select to test for a (non-blocking) connection success crashes on FreeBSD when the number of opened file descriptor is higher than FDSET_SIZE.
Instead of returning with an error in that case, this commit uses poll instead that's not limited to the number of opened file descriptors, preventing an out-of-bound write.
  • Loading branch information
X-Ryl669 committed Oct 2, 2023
1 parent 8528073 commit ab790c2
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/mdns_avahi.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <net/if.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>

#include <event2/event.h>

Expand Down Expand Up @@ -582,8 +583,7 @@ connection_test(int family, const char *address, const char *address_log, int po
struct addrinfo *ai;
char strport[32];
int sock;
fd_set fdset;
struct timeval timeout = { MDNS_CONNECT_TEST_TIMEOUT, 0 };
struct pollfd fd;
socklen_t len;
int flags;
int error;
Expand Down Expand Up @@ -639,10 +639,11 @@ connection_test(int family, const char *address, const char *address_log, int po
// the case, but FreeBSD connect() sometimes returns immediate success.
if (ret != 0)
{
FD_ZERO(&fdset);
FD_SET(sock, &fdset);

ret = select(sock + 1, NULL, &fdset, NULL, &timeout);
// Use poll here since select requires using fdset that would be overflowed in FreeBSD
fd.fd = sock;
fd.events = POLLOUT;

ret = poll(&fd, 1, MDNS_CONNECT_TEST_TIMEOUT * 1000);
if (ret < 0)
{
DPRINTF(E_WARN, L_MDNS, "Connection test to %s:%d failed with select error: %s\n", address_log, port, strerror(errno));
Expand Down

0 comments on commit ab790c2

Please sign in to comment.