1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-10 17:59:04 +00:00

Make removing fd from fd_list_t a little more efficient

This commit is contained in:
dmiller
2022-10-14 00:58:22 +00:00
parent 6a9acb1f1a
commit 349da3b98a

View File

@@ -603,32 +603,41 @@ int add_fd(fd_list_t *fdl, int fd)
int rm_fd(fd_list_t *fdl, int fd)
{
int x = 0, last = fdl->nfds;
int found = -1;
int newfdmax = 0;
/* make sure we have a list */
if (last == 0)
bye("Program bug: Trying to remove fd from list with no fds.");
/* find the fd in the list */
for (x = 0; x < last; x++)
if (fdl->fds[x].fd == fd)
break;
for (x = 0; x < last; x++) {
struct fdinfo *fdi = &fdl->fds[x];
if (fdi->fd == fd) {
found = x;
/* If it's not the max, we can bail early. */
if (fd < fdl->fdmax) {
newfdmax = fdl->fdmax;
break;
}
}
else if (fdi->fd > newfdmax)
newfdmax = fdi->fd;
}
fdl->fdmax = newfdmax;
/* make sure we found it */
if (x == last)
if (found < 0)
bye("Program bug: fd (%d) not on list.", fd);
/* remove it, does nothing if (last == 1) */
if (o.debug > 1)
logdebug("Swapping fd[%d] (%d) with fd[%d] (%d)\n",
x, fdl->fds[x].fd, last - 1, fdl->fds[last - 1].fd);
fdl->fds[x] = fdl->fds[last - 1];
found, fdl->fds[found].fd, last - 1, fdl->fds[last - 1].fd);
fdl->fds[found] = fdl->fds[last - 1];
fdl->nfds--;
/* was it the max */
if (fd == fdl->fdmax)
fdl->fdmax = get_maxfd(fdl);
if (o.debug > 1)
logdebug("Removed fd %d from list, nfds %d, maxfd %d\n", fd, fdl->nfds, fdl->fdmax);
return 0;