Index: ossp-pkg/pth/ChangeLog RCS File: /v/ossp/cvs/ossp-pkg/pth/ChangeLog,v rcsdiff -q -kk '-r1.556' '-r1.557' -u '/v/ossp/cvs/ossp-pkg/pth/ChangeLog,v' 2>/dev/null --- ChangeLog 2002/01/27 12:39:10 1.556 +++ ChangeLog 2002/01/27 13:15:28 1.557 @@ -21,6 +21,14 @@ Changes between 1.4.0 and 1.4.1 (24-Mar-2001 to 27-Jan-2002) + *) Internally make sure an invalid file-descriptor (integer not + between 0 and (FD_SETSIZE-1) does not lead to any segfaults or + other undefined behaviour. Instead an error is returned and errno + is set to EBADF, similar to what the OS functions do. Especially + pth_poll() now return with this error (instead of skipping the fd) + if an fd in the "struct pollfd" is invalid. + [Ralf S. Engelschall, Archie Cobbs ] + *) Correctly support PTH_FDMODE_NONBLOCK in pth_connect and pth_accept. [Archie Cobbs ] Index: ossp-pkg/pth/pth_event.c RCS File: /v/ossp/cvs/ossp-pkg/pth/pth_event.c,v rcsdiff -q -kk '-r1.57' '-r1.58' -u '/v/ossp/cvs/ossp-pkg/pth/pth_event.c,v' 2>/dev/null --- pth_event.c 2002/01/27 11:03:40 1.57 +++ pth_event.c 2002/01/27 13:15:28 1.58 @@ -114,6 +114,8 @@ if (spec & PTH_EVENT_FD) { /* filedescriptor event */ int fd = va_arg(ap, int); + if (!pth_util_fd_valid(fd)) + return_errno(NULL, EBADF); ev->ev_type = PTH_EVENT_FD; ev->ev_goal = (int)(spec & (PTH_UNTIL_FD_READABLE|\ PTH_UNTIL_FD_WRITEABLE|\ Index: ossp-pkg/pth/pth_high.c RCS File: /v/ossp/cvs/ossp-pkg/pth/pth_high.c,v rcsdiff -q -kk '-r1.82' '-r1.83' -u '/v/ossp/cvs/ossp-pkg/pth/pth_high.c,v' 2>/dev/null --- pth_high.c 2002/01/27 12:39:10 1.82 +++ pth_high.c 2002/01/27 13:15:28 1.83 @@ -411,8 +411,8 @@ FD_ZERO(&wfds); FD_ZERO(&efds); for(i = 0; i < nfd; i++) { - if (pfd[i].fd < 0) - continue; + if (!pth_util_fd_valid(pfd[i].fd)) + return_errno(-1, EBADF); if (pfd[i].events & POLLIN) FD_SET(pfd[i].fd, &rfds); if (pfd[i].events & POLLOUT) @@ -600,6 +600,8 @@ /* now directly poll filedescriptor for readability to avoid unneccessary (and resource consuming because of context switches, etc) event handling through the scheduler */ + if (!pth_util_fd_valid(fd)) + return_errno(-1, EBADF); FD_ZERO(&fds); FD_SET(fd, &fds); delay.tv_sec = 0; @@ -668,6 +670,10 @@ /* now directly poll filedescriptor for writeability to avoid unneccessary (and resource consuming because of context switches, etc) event handling through the scheduler */ + if (!pth_util_fd_valid(fd)) { + pth_fdmode(fd, fdmode); + return_errno(-1, EBADF); + } FD_ZERO(&fds); FD_SET(fd, &fds); delay.tv_sec = 0; @@ -758,6 +764,8 @@ /* first directly poll filedescriptor for readability to avoid unneccessary (and resource consuming because of context switches, etc) event handling through the scheduler */ + if (!pth_util_fd_valid(fd)) + return_errno(-1, EBADF); FD_ZERO(&fds); FD_SET(fd, &fds); delay.tv_sec = 0; @@ -900,6 +908,12 @@ /* first directly poll filedescriptor for writeability to avoid unneccessary (and resource consuming because of context switches, etc) event handling through the scheduler */ + if (!pth_util_fd_valid(fd)) { + pth_fdmode(fd, fdmode); + if (iovcnt > sizeof(tiov_stack)) + free(tiov); + return_errno(-1, EBADF); + } FD_ZERO(&fds); FD_SET(fd, &fds); delay.tv_sec = 0; @@ -1177,6 +1191,8 @@ /* now directly poll filedescriptor for readability to avoid unneccessary (and resource consuming because of context switches, etc) event handling through the scheduler */ + if (!pth_util_fd_valid(fd)) + return_errno(-1, EBADF); FD_ZERO(&fds); FD_SET(fd, &fds); delay.tv_sec = 0; @@ -1257,6 +1273,10 @@ /* now directly poll filedescriptor for writeability to avoid unneccessary (and resource consuming because of context switches, etc) event handling through the scheduler */ + if (!pth_util_fd_valid(fd)) { + pth_fdmode(fd, fdmode); + return_errno(-1, EBADF); + } FD_ZERO(&fds); FD_SET(fd, &fds); delay.tv_sec = 0; Index: ossp-pkg/pth/pth_util.c RCS File: /v/ossp/cvs/ossp-pkg/pth/pth_util.c,v rcsdiff -q -kk '-r1.20' '-r1.21' -u '/v/ossp/cvs/ossp-pkg/pth/pth_util.c,v' 2>/dev/null --- pth_util.c 2002/01/27 11:03:41 1.20 +++ pth_util.c 2002/01/27 13:15:28 1.21 @@ -91,6 +91,15 @@ return d; } +/* check whether a file-descriptor is valid */ +#if cpp +#if !defined(FD_SETSIZE) +#define FD_SETSIZE 1024 +#endif +#define pth_util_fd_valid(fd) \ + ((fd) >= 0 && (fd) <= (FD_SETSIZE-1)) +#endif + /* merge input fd set into output fds */ intern void pth_util_fds_merge(int nfd, fd_set *ifds1, fd_set *ofds1,