OSSP CVS Repository

ossp - Check-in [2657]
Not logged in
[Honeypot]  [Browse]  [Home]  [Login]  [Reports
[Search]  [Ticket]  [Timeline
  [Patchset]  [Tagging/Branching

Check-in Number: 2657
Date: 2002-Oct-24 11:07:51 (local)
2002-Oct-24 09:07:51 (UTC)
User:rse
Branch:
Comment: Added POSIX-compliant sanity checks for bad filedescriptors to mostly all filedescriptor-based I/O functions in pth_high.c
Tickets:
Inspections:
Files:
ossp-pkg/pth/ChangeLog      1.577 -> 1.578     4 inserted, 0 deleted
ossp-pkg/pth/pth_high.c      1.89 -> 1.90     22 inserted, 16 deleted
ossp-pkg/pth/pth_p.h.in      1.33 -> 1.34     5 inserted, 0 deleted
ossp-pkg/pth/pth_util.c      1.21 -> 1.22     8 inserted, 7 deleted

ossp-pkg/pth/ChangeLog 1.577 -> 1.578

--- 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 <skrll@netbsd.org>, Ralf S. Engelschall]
 


ossp-pkg/pth/pth_high.c 1.89 -> 1.90

--- 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);


ossp-pkg/pth/pth_p.h.in 1.33 -> 1.34

--- 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 {


ossp-pkg/pth/pth_util.c 1.21 -> 1.22

--- 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,

CVSTrac 2.0.1