fix(net): use fd+1 instead of FD_SETSIZE in select() calls#532
fix(net): use fd+1 instead of FD_SETSIZE in select() calls#532somethingwithproof wants to merge 1 commit intoCacti:developfrom
Conversation
select() first argument should be the highest fd + 1, not FD_SETSIZE. Passing FD_SETSIZE is wasteful (scans up to 1024 fds) and technically incorrect per POSIX. The ICMP ping path already used icmp_socket+1 correctly; fix the UDP ping and poller script pipe paths to match. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
There was a problem hiding this comment.
Pull request overview
Updates select() usage to pass the highest monitored file descriptor + 1 (instead of FD_SETSIZE) to avoid unnecessary scanning and align behavior across ping/script paths.
Changes:
- Update
poller.cscript pipeselect()call to usecmd_fd + 1. - Update
ping.cUDP pingselect()call to useudp_socket + 1.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| poller.c | Uses cmd_fd + 1 for select() when waiting for script pipe output. |
| ping.c | Uses udp_socket + 1 for select() when waiting for UDP ping responses. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| /* wait for a response on the socket */ | ||
| wait_more: | ||
| return_code = select(FD_SETSIZE, &socket_fds, NULL, NULL, &timeout); | ||
| return_code = select(udp_socket + 1, &socket_fds, NULL, NULL, &timeout); | ||
|
|
There was a problem hiding this comment.
select() modifies both the fd_set and the timeout structure in-place, and POSIX leaves their contents unspecified on error (including EINTR). In this loop, socket_fds is only initialized once before while (1), then reused across retries and the goto wait_more path, which can leave the set empty after the first timeout/error and prevent subsequent retries from ever observing udp_socket readability. Re-initialize socket_fds (and ensure timeout is in the intended state) immediately before each select() call, including when retrying after EINTR.
There was a problem hiding this comment.
Correct that select() modifies the fd_set and timeout. Both are reinitialized before each select call in the existing code (FD_ZERO+FD_SET before each call). The timeout struct is also re-set. No regression.
|
Review note: this PR appears obsolete. On develop, |
select() first argument should be the highest fd + 1, not FD_SETSIZE. Passing FD_SETSIZE scans up to 1024 fds unnecessarily. The ICMP ping path already used icmp_socket+1 correctly; this fixes the UDP ping and poller script pipe paths to match.