]> git.kaiwu.me - nginx.git/commitdiff
Events: validate descriptor before FD_SET()
authorSourav Bhowmik <s.bhowmik@f5.com>
Wed, 22 Jul 2026 16:12:28 +0000 (09:12 -0700)
committerSergey Kandaurov <s.kandaurov@f5.com>
Fri, 24 Jul 2026 16:31:03 +0000 (20:31 +0400)
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.

src/event/modules/ngx_select_module.c

index b9fceb3b648be2f45a3ad5f293d7c2c78bfa23c4..96bfe4420fe187fa0bb86a9d1f6697783ead0ab1 100644 (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 "