summaryrefslogtreecommitdiff
path: root/openbsd-compat
diff options
context:
space:
mode:
authorPierre Ossman <ossman@cendio.se>2022-07-06 13:52:10 +0200
committerDarren Tucker <dtucker@dtucker.net>2022-11-08 19:33:47 +1100
commitd9df5689c29823ab830ec4f54c83c6cc3c0077ad (patch)
tree5488325053408fd30726279a90d06e3079d82954 /openbsd-compat
parent419aa8a312e8d8f491933ca3d5933e602cb05aae (diff)
downloadopenssh-git-d9df5689c29823ab830ec4f54c83c6cc3c0077ad.tar.gz
Avoid assuming layout of fd_set
POSIX doesn't specify the internal layout of the fd_set object, so let's not assume it is just a bit mask. This increases compatibility with systems that have a different layout. The assumption is also worthless as we already refuse to use file descriptors over FD_SETSIZE anyway. Meaning that the default size of fd_set is quite sufficient.
Diffstat (limited to 'openbsd-compat')
-rw-r--r--openbsd-compat/bsd-poll.c38
1 files changed, 12 insertions, 26 deletions
diff --git a/openbsd-compat/bsd-poll.c b/openbsd-compat/bsd-poll.c
index 9a9794f5..967f947b 100644
--- a/openbsd-compat/bsd-poll.c
+++ b/openbsd-compat/bsd-poll.c
@@ -47,9 +47,8 @@ ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *tmoutp,
const sigset_t *sigmask)
{
nfds_t i;
- int saved_errno, ret, fd, maxfd = 0;
- fd_set *readfds = NULL, *writefds = NULL, *exceptfds = NULL;
- size_t nmemb;
+ int ret, fd, maxfd = 0;
+ fd_set readfds, writefds, exceptfds;
for (i = 0; i < nfds; i++) {
fd = fds[i].fd;
@@ -60,30 +59,23 @@ ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *tmoutp,
maxfd = MAX(maxfd, fd);
}
- nmemb = howmany(maxfd + 1 , NFDBITS);
- if ((readfds = calloc(nmemb, sizeof(fd_mask))) == NULL ||
- (writefds = calloc(nmemb, sizeof(fd_mask))) == NULL ||
- (exceptfds = calloc(nmemb, sizeof(fd_mask))) == NULL) {
- saved_errno = ENOMEM;
- ret = -1;
- goto out;
- }
-
/* populate event bit vectors for the events we're interested in */
+ FD_ZERO(&readfds);
+ FD_ZERO(&writefds);
+ FD_ZERO(&exceptfds);
for (i = 0; i < nfds; i++) {
fd = fds[i].fd;
if (fd == -1)
continue;
if (fds[i].events & POLLIN)
- FD_SET(fd, readfds);
+ FD_SET(fd, &readfds);
if (fds[i].events & POLLOUT)
- FD_SET(fd, writefds);
+ FD_SET(fd, &writefds);
if (fds[i].events & POLLPRI)
- FD_SET(fd, exceptfds);
+ FD_SET(fd, &exceptfds);
}
- ret = pselect(maxfd + 1, readfds, writefds, exceptfds, tmoutp, sigmask);
- saved_errno = errno;
+ ret = pselect(maxfd + 1, &readfds, &writefds, &exceptfds, tmoutp, sigmask);
/* scan through select results and set poll() flags */
for (i = 0; i < nfds; i++) {
@@ -91,20 +83,14 @@ ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *tmoutp,
fds[i].revents = 0;
if (fd == -1)
continue;
- if ((fds[i].events & POLLIN) && FD_ISSET(fd, readfds))
+ if ((fds[i].events & POLLIN) && FD_ISSET(fd, &readfds))
fds[i].revents |= POLLIN;
- if ((fds[i].events & POLLOUT) && FD_ISSET(fd, writefds))
+ if ((fds[i].events & POLLOUT) && FD_ISSET(fd, &writefds))
fds[i].revents |= POLLOUT;
- if ((fds[i].events & POLLPRI) && FD_ISSET(fd, exceptfds))
+ if ((fds[i].events & POLLPRI) && FD_ISSET(fd, &exceptfds))
fds[i].revents |= POLLPRI;
}
-out:
- free(readfds);
- free(writefds);
- free(exceptfds);
- if (ret == -1)
- errno = saved_errno;
return ret;
}
#endif /* !HAVE_PPOLL || BROKEN_POLL */