Index: ossp-pkg/pth/ChangeLog RCS File: /v/ossp/cvs/ossp-pkg/pth/ChangeLog,v rcsdiff -q -kk '-r1.577' '-r1.578' -u '/v/ossp/cvs/ossp-pkg/pth/ChangeLog,v' 2>/dev/null --- ChangeLog 2002/10/23 14:04:00 1.577 +++ ChangeLog 2002/10/24 09:07:51 1.578 @@ -21,6 +21,10 @@ Changes between 1.4.1 and 1.5.0 (27-Jan-2002 to xx-Oct-2002) + *) Added POSIX-compliant sanity checks for bad filedescriptors + to mostly all filedescriptor-based I/O functions in pth_high.c + [Ralf S. Engelschall] + *) Added pth_nanosleep() function. [Nick Hudson , Ralf S. Engelschall] Index: ossp-pkg/pth/pth_high.c RCS File: /v/ossp/cvs/ossp-pkg/pth/pth_high.c,v rcsdiff -q -kk '-r1.89' '-r1.90' -u '/v/ossp/cvs/ossp-pkg/pth/pth_high.c,v' 2>/dev/null --- pth_high.c 2002/10/23 14:04:00 1.89 +++ pth_high.c 2002/10/24 09:07:51 1.90 @@ -297,8 +297,8 @@ pth_implicit_init(); pth_debug2("pth_select_ev: called from thread \"%s\"", pth_current->name); - /* sanity checking */ - if (!pth_util_fd_valid(nfd-1)) + /* POSIX compliance */ + if (nfd < 0 || nfd > FD_SETSIZE) return_errno(-1, EINVAL); /* first deal with the special situation of a plain microsecond delay */ @@ -544,6 +544,10 @@ pth_implicit_init(); pth_debug2("pth_connect_ev: enter from thread \"%s\"", pth_current->name); + /* POSIX compliance */ + if (!pth_util_fd_valid(s)) + return_errno(-1, EBADF); + /* force filedescriptor into non-blocking mode */ fdmode = pth_fdmode(s, PTH_FDMODE_NONBLOCK); @@ -595,6 +599,10 @@ pth_implicit_init(); pth_debug2("pth_accept_ev: enter from thread \"%s\"", pth_current->name); + /* POSIX compliance */ + if (!pth_util_fd_valid(s)) + return_errno(-1, EBADF); + /* force filedescriptor into non-blocking mode */ fdmode = pth_fdmode(s, PTH_FDMODE_NONBLOCK); @@ -653,6 +661,8 @@ /* POSIX compliance */ if (nbytes == 0) return 0; + if (!pth_util_fd_valid(fd)) + return_errno(-1, EBADF); /* poll filedescriptor if not already in non-blocking operation */ if (pth_fdmode(fd, PTH_FDMODE_POLL) == PTH_FDMODE_BLOCK) { @@ -660,8 +670,6 @@ /* 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; @@ -720,6 +728,8 @@ /* POSIX compliance */ if (nbytes == 0) return 0; + if (!pth_util_fd_valid(fd)) + return_errno(-1, EBADF); /* force filedescriptor into non-blocking mode */ fdmode = pth_fdmode(fd, PTH_FDMODE_NONBLOCK); @@ -730,10 +740,6 @@ /* 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; @@ -817,6 +823,8 @@ /* POSIX compliance */ if (iovcnt <= 0 || iovcnt > UIO_MAXIOV) return_errno(-1, EINVAL); + if (!pth_util_fd_valid(fd)) + return_errno(-1, EBADF); /* poll filedescriptor if not already in non-blocking operation */ if (pth_fdmode(fd, PTH_FDMODE_POLL) == PTH_FDMODE_BLOCK) { @@ -824,8 +832,6 @@ /* 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; @@ -939,6 +945,8 @@ /* POSIX compliance */ if (iovcnt <= 0 || iovcnt > UIO_MAXIOV) return_errno(-1, EINVAL); + if (!pth_util_fd_valid(fd)) + return_errno(-1, EBADF); /* force filedescriptor into non-blocking mode */ fdmode = pth_fdmode(fd, PTH_FDMODE_NONBLOCK); @@ -968,12 +976,6 @@ /* 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; @@ -1244,6 +1246,8 @@ /* POSIX compliance */ if (nbytes == 0) return 0; + if (!pth_util_fd_valid(fd)) + return_errno(-1, EBADF); /* poll filedescriptor if not already in non-blocking operation */ if (pth_fdmode(fd, PTH_FDMODE_POLL) == PTH_FDMODE_BLOCK) { @@ -1323,6 +1327,8 @@ /* POSIX compliance */ if (nbytes == 0) return 0; + if (!pth_util_fd_valid(fd)) + return_errno(-1, EBADF); /* force filedescriptor into non-blocking mode */ fdmode = pth_fdmode(fd, PTH_FDMODE_NONBLOCK); Index: ossp-pkg/pth/pth_p.h.in RCS File: /v/ossp/cvs/ossp-pkg/pth/pth_p.h.in,v rcsdiff -q -kk '-r1.33' '-r1.34' -u '/v/ossp/cvs/ossp-pkg/pth/pth_p.h.in,v' 2>/dev/null --- pth_p.h.in 2002/10/23 14:04:00 1.33 +++ pth_p.h.in 2002/10/24 09:07:51 1.34 @@ -104,6 +104,11 @@ #endif #endif +/* fallback definition for fdset_t size */ +#if !defined(FD_SETSIZE) +#define FD_SETSIZE 1024 +#endif + /* fallback definition for struct timespec */ #ifndef HAVE_STRUCT_TIMESPEC struct timespec { Index: ossp-pkg/pth/pth_util.c RCS File: /v/ossp/cvs/ossp-pkg/pth/pth_util.c,v rcsdiff -q -kk '-r1.21' '-r1.22' -u '/v/ossp/cvs/ossp-pkg/pth/pth_util.c,v' 2>/dev/null --- pth_util.c 2002/01/27 13:15:28 1.21 +++ pth_util.c 2002/10/24 09:07:51 1.22 @@ -92,13 +92,14 @@ } /* 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 +intern int pth_util_fd_valid(int fd) +{ + if (fd < 0 || fd >= FD_SETSIZE) + return FALSE; + if (fcntl(fd, F_GETFL) == -1 && errno == EBADF) + return FALSE; + return TRUE; +} /* merge input fd set into output fds */ intern void pth_util_fds_merge(int nfd,