Permalink
Browse files

replace getdtablesize() and FD_SETSIZE with maxfd+1

Found by: michaelortmann
Patch by: michaelortmann
  • Loading branch information...
michaelortmann authored and vanosg committed Aug 28, 2018
1 parent fbd763a commit e1b3c9dd0921047d8e58a6d23d9293b345b7002e
Showing with 29 additions and 42 deletions.
  1. +0 −3 config.h.in
  2. +1 −1 configure.ac
  3. +28 −37 src/net.c
  4. +0 −1 src/proto.h
View
@@ -99,9 +99,6 @@
/* Define to 1 if you have the `fsync' function. */
#undef HAVE_FSYNC
/* Define to 1 if you have the `getdtablesize' function. */
#undef HAVE_GETDTABLESIZE
/* Define to 1 if you have the `gethostbyname2' function. */
#undef HAVE_GETHOSTBYNAME2
View
@@ -109,7 +109,7 @@ EGG_CHECK_SOCKLEN_T
AX_CREATE_STDINT_H([eggint.h])
# Checks for functions and their arguments.
AC_CHECK_FUNCS([clock dprintf fsync getdtablesize getrandom getrusage inet_aton inet_ntop isascii mbrlen memcpy memset random rand lrand48 rename setpgid sigaction sigemptyset snprintf strcasecmp strncasecmp uname vsnprintf])
AC_CHECK_FUNCS([clock dprintf fsync getrandom getrusage inet_aton inet_ntop isascii mbrlen memcpy memset random rand lrand48 rename setpgid sigaction sigemptyset snprintf strcasecmp strncasecmp uname vsnprintf])
AC_FUNC_SELECT_ARGTYPES
EGG_FUNC_VPRINTF
AC_FUNC_STRFTIME
View
@@ -46,14 +46,6 @@
# include <openssl/err.h>
#endif
#ifndef HAVE_GETDTABLESIZE
# ifdef FD_SETSIZE
# define getdtablesize() FD_SETSIZE
# else
# define getdtablesize() 200
# endif
#endif
extern struct dcc_t *dcc;
extern int backgrd, use_stderr, resolve_timeout, dcc_total;
extern unsigned long otraffic_irc_today, otraffic_bn_today, otraffic_dcc_today,
@@ -760,17 +752,17 @@ int getdccfamilyaddr(sockname_t *addr, char *s, size_t l, int restrict_af)
* sockets, but tcl also cares for writable/exceptions.
* preparefdset() can be called by Tcl Threads
*/
int preparefdset(fd_set *fd, sock_list *slist, int slistmax, int tclonly, int tclmask)
static int preparefdset(fd_set *fds, sock_list *slist, int slistmax, int tclonly, int tclmask)
{
int fdtmp, i, foundsocks = 0;
int fd, i, nfds = 0;
FD_ZERO(fd);
FD_ZERO(fds);
for (i = 0; i < slistmax; i++) {
if (!(slist[i].flags & (SOCK_UNUSED | SOCK_VIRTUAL))) {
if ((slist[i].sock == STDOUT) && !backgrd)
fdtmp = STDIN;
fd = STDIN;
else
fdtmp = slist[i].sock;
fd = slist[i].sock;
/*
* Looks like that having more than a call, in the same
* program, to the FD_SET macro, triggers a bug in gcc.
@@ -785,11 +777,12 @@ int preparefdset(fd_set *fd, sock_list *slist, int slistmax, int tclonly, int tc
continue;
} else if (tclonly)
continue;
foundsocks = 1;
FD_SET(fdtmp, fd);
if (fd > nfds)
nfds = fd;
FD_SET(fd, fds);
}
}
return foundsocks;
return nfds;
}
/* A safer version of write() that deals with partial writes. */
@@ -825,28 +818,29 @@ int sockread(char *s, int *len, sock_list *slist, int slistmax, int tclonly)
{
struct timeval t;
fd_set fdr, fdw, fde;
int fds, i, x, have_r, have_w, have_e;
int i, x, nfds_r, nfds_w, nfds_e;
int grab = 511, tclsock = -1, events = 0;
struct threaddata *td = threaddata();
int nfds;
fds = getdtablesize();
#ifdef FD_SETSIZE
if (fds > FD_SETSIZE)
fds = FD_SETSIZE; /* Fixes YET ANOTHER freebsd bug!!! */
#endif
nfds_r = preparefdset(&fdr, slist, slistmax, tclonly, TCL_READABLE);
nfds_w = preparefdset(&fdw, slist, slistmax, 1, TCL_WRITABLE);
nfds_e = preparefdset(&fde, slist, slistmax, 1, TCL_EXCEPTION);
have_r = preparefdset(&fdr, slist, slistmax, tclonly, TCL_READABLE);
have_w = preparefdset(&fdw, slist, slistmax, 1, TCL_WRITABLE);
have_e = preparefdset(&fde, slist, slistmax, 1, TCL_EXCEPTION);
nfds = nfds_r;
if (nfds_w > nfds)
nfds = nfds_w;
if (nfds_e > nfds)
nfds = nfds_e;
/* select() may modify the timeval argument - copy it */
t.tv_sec = td->blocktime.tv_sec;
t.tv_usec = td->blocktime.tv_usec;
x = select((SELECT_TYPE_ARG1) fds,
SELECT_TYPE_ARG234 (have_r ? &fdr : NULL),
SELECT_TYPE_ARG234 (have_w ? &fdw : NULL),
SELECT_TYPE_ARG234 (have_e ? &fde : NULL),
x = select((SELECT_TYPE_ARG1) nfds + 1,
SELECT_TYPE_ARG234 (nfds_r ? &fdr : NULL),
SELECT_TYPE_ARG234 (nfds_w ? &fdw : NULL),
SELECT_TYPE_ARG234 (nfds_e ? &fde : NULL),
SELECT_TYPE_ARG5 &t);
if (x == -1)
return -2; /* socket error */
@@ -1256,31 +1250,28 @@ void tputs(int z, char *s, unsigned int len)
void dequeue_sockets()
{
int i, x;
int z = 0, fds;
int z = 0;
fd_set wfds;
struct timeval tv;
int nfds = 0;
/* ^-- start poptix test code, this should avoid writes to sockets not ready to be written to. */
fds = getdtablesize();
#ifdef FD_SETSIZE
if (fds > FD_SETSIZE)
fds = FD_SETSIZE; /* Fixes YET ANOTHER freebsd bug!!! */
#endif
FD_ZERO(&wfds);
tv.tv_sec = 0;
tv.tv_usec = 0; /* we only want to see if it's ready for writing, no need to actually wait.. */
for (i = 0; i < threaddata()->MAXSOCKS; i++)
if (!(socklist[i].flags & (SOCK_UNUSED | SOCK_TCL)) &&
(socklist[i].handler.sock.outbuf != NULL)) {
if (socklist[i].sock > nfds)
nfds = socklist[i].sock;
FD_SET(socklist[i].sock, &wfds);
z = 1;
}
if (!z)
return; /* nothing to write */
select((SELECT_TYPE_ARG1) fds, SELECT_TYPE_ARG234 NULL,
select((SELECT_TYPE_ARG1) nfds + 1, SELECT_TYPE_ARG234 NULL,
SELECT_TYPE_ARG234 &wfds, SELECT_TYPE_ARG234 NULL,
SELECT_TYPE_ARG5 &tv);
View
@@ -284,7 +284,6 @@ int getdccaddr(sockname_t *, char *, size_t);
int getdccfamilyaddr(sockname_t *, char *, size_t, int);
void tputs(int, char *, unsigned int);
void dequeue_sockets();
int preparefdset(fd_set *, sock_list *, int, int, int);
int sockread(char *, int *, sock_list *, int, int);
int sockgets(char *, int *);
void tell_netdebug(int);

0 comments on commit e1b3c9d

Please sign in to comment.