Events: validate descriptor before FD_SET()

The check in ngx_select_init_conf() can be insufficient because it
doesn't cover descriptors for local files, which are not accounted
for in cycle->connection_n.  For instance, to reach this condition
with new connections, no reusable connections must be available for
recycling in ngx_get_connection().

The fix adopts a similar code from the win32 select module.
This commit is contained in:
Sourav Bhowmik 2026-07-22 09:12:28 -07:00 committed by Sergey Kandaurov
parent 5e0deb7018
commit 0cb3d7fb13

View file

@ -146,6 +146,17 @@ ngx_select_add_event(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags)
return NGX_ERROR;
}
/* disable warning: the default FD_SETSIZE is 1024U in FreeBSD 5.x-10.x */
if ((event == NGX_READ_EVENT || event == NGX_WRITE_EVENT)
&& (unsigned) c->fd >= FD_SETSIZE)
{
ngx_log_error(NGX_LOG_ERR, ev->log, 0,
"maximum number of descriptors "
"supported by select() is %ud", FD_SETSIZE);
return NGX_ERROR;
}
if (event == NGX_READ_EVENT) {
FD_SET(c->fd, &master_read_fd_set);
@ -411,8 +422,6 @@ ngx_select_init_conf(ngx_cycle_t *cycle, void *conf)
return NGX_CONF_OK;
}
/* disable warning: the default FD_SETSIZE is 1024U in FreeBSD 5.x */
if (cycle->connection_n > FD_SETSIZE) {
ngx_log_error(NGX_LOG_EMERG, cycle->log, 0,
"the maximum number of files "