wutsocket fixes: select() handles timeout wrong, and poll() lacks a safety check (#428)
Some checks failed
C/C++ CI / ubuntu-latest (push) Has been cancelled

* - Fix select() to also update the fd_set arguments on timeout condition.
- Move the nfds argument restriction to the nsysnet side.
- Allow user-defined FD_SETSIZE, since newlib's fd_set allow custom sizes.
- Define __socklen_t_defined when socklen_t is typedefed.

* Added safety check for poll(): the nsysnet fd must fit in nsysnet_fd_set.

---------

Co-authored-by: Daniel K. O. (dkosmari) <none@none>
This commit is contained in:
Daniel K. O. 2025-10-10 15:27:42 -03:00 committed by GitHub
parent cac70f560e
commit 1b412d2769
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 20 additions and 8 deletions

View File

@ -1,5 +1,7 @@
#pragma once
#ifndef FD_SETSIZE
#define FD_SETSIZE 32
#endif
#include_next <sys/select.h>

View File

@ -53,7 +53,11 @@
#define SO_NOSLOWSTART 0x4000 // disable slowstart
#define SO_RUSRBUF 0x10000 // enable somemopt provided memory for receive buffer
typedef uint32_t socklen_t;
#ifndef __socklen_t_defined
typedef __socklen_t socklen_t;
#define __socklen_t_defined
#endif
typedef uint16_t sa_family_t;
struct sockaddr

View File

@ -32,6 +32,10 @@ poll(struct pollfd *fds,
if ((cnv_fd + 1) > cnv_nfds) {
cnv_nfds = cnv_fd + 1;
if (cnv_nfds > NSYSNET_FD_SETSIZE) {
errno = EINVAL;
return -1;
}
}
if (fds[i].events & POLLIN) {

View File

@ -11,11 +11,6 @@ select(int nfds,
nsysnet_fd_set cnv_rd, cnv_wr, cnv_ex;
struct nsysnet_timeval cnv_timeout;
if (nfds > FD_SETSIZE) {
errno = EINVAL;
return -1;
}
NSYSNET_FD_ZERO(&cnv_rd);
NSYSNET_FD_ZERO(&cnv_wr);
NSYSNET_FD_ZERO(&cnv_ex);
@ -38,6 +33,10 @@ select(int nfds,
if ((cnv_fd + 1) > cnv_nfds) {
cnv_nfds = cnv_fd + 1;
if (cnv_nfds > NSYSNET_FD_SETSIZE) {
errno = EINVAL;
return -1;
}
}
if (rd_fd) {
@ -67,8 +66,6 @@ select(int nfds,
return rc;
}
rc = 0;
if (readfds) {
FD_ZERO(readfds);
}
@ -79,6 +76,11 @@ select(int nfds,
FD_ZERO(exceptfds);
}
if (rc == 0)
return 0;
rc = 0;
for (i = 0; i < nfds; i++) {
int cnv_fd = __wut_get_nsysnet_fd(i);
if (cnv_fd == -1) {