Bug 188439: fixed a bug in the PR_Poll implementation for NSPRPUB_PRE_4_2_CLIENT_BRANCH
authorwtc%netscape.com
Mon, 20 Jan 2003 20:50:00 +0000
branchNSPRPUB_PRE_4_2_CLIENT_BRANCH
changeset 2668 e6a72f360a5aca6db18dfbd5bae4b039750948e8
parent 2667 3c32f0ea819ec54721d3a87a8accf9a66eab9277
child 2670 fc67c6f297653ddf46e26420a604d804a17596be
push idunknown
push userunknown
push dateunknown
bugs188439
Bug 188439: fixed a bug in the PR_Poll implementation for _PR_POLL_WITH_SELECT. We could still call FD_SET if the osfd was larger than FD_SETSIZE. Fixed the bug that _PR_POLL_WITH_SELECT was defined after the system header files had been included. Need to define it on the compile command line. Define _USE_BIG_FDS on HP-UX. Because that macro increases the size of fd_set to about 7500 bytes and PR_Poll allocates 3 fd_sets on the stack, also increased the minimum thread stack size to 128KB on HP-UX. Modified Files: configure configure.in _hpux.h _osf1.h ptio.c Tag: NSPRPUB_PRE_4_2_CLIENT_BRANCH
configure
configure.in
pr/include/md/_hpux.h
pr/include/md/_osf1.h
pr/src/pthreads/ptio.c
--- a/configure
+++ b/configure
@@ -3323,16 +3323,28 @@ EOF
     cat >> confdefs.h <<\EOF
 #define _HPUX_SOURCE 1
 EOF
 
     cat >> confdefs.h <<\EOF
 #define hppa 1
 EOF
 
+    # OSF1 and HPUX report the POLLHUP event for a socket when the
+    # shutdown(SHUT_WR) operation is called for the remote end, even though
+    # the socket is still writeable. Use select(), instead of poll(), to
+    # workaround this problem.
+    cat >> confdefs.h <<\EOF
+#define _PR_POLL_WITH_SELECT 1
+EOF
+
+    cat >> confdefs.h <<\EOF
+#define _USE_BIG_FDS 1
+EOF
+
     DLL_SUFFIX=sl
     DSO_LDOPTS='-b +h $(notdir $@)'
     PR_MD_CSRCS=hpux.c
     if test "$OS_TEST" != "ia64"; then
         PR_MD_ASFILES=os_HPUX.s
     fi
     if test -n "$USE_64"; then
         MDCPUCFG_H=_hpux64.cfg
@@ -4142,40 +4154,48 @@ EOF
     cat >> confdefs.h <<\EOF
 #define OSF1 1
 EOF
 
     cat >> confdefs.h <<\EOF
 #define _REENTRANT 1
 EOF
 
+    # OSF1 and HPUX report the POLLHUP event for a socket when the
+    # shutdown(SHUT_WR) operation is called for the remote end, even though
+    # the socket is still writeable. Use select(), instead of poll(), to
+    # workaround this problem.
+    cat >> confdefs.h <<\EOF
+#define _PR_POLL_WITH_SELECT 1
+EOF
+
 
     if echo "$OS_RELEASE" | egrep -c '(V2.0|V3.2)' 2>/dev/null ; then
         USE_NSPR_THREADS=1
     fi
 
     if test -z "$GNU_CC"; then
         CC="$CC -std1 -ieee_with_inexact"
         if test "$OS_RELEASE" != "V2.0"; then
             CC="$CC -readonly_strings"
         fi
         _OPTIMIZE_FLAGS="$_OPTIMIZE_FLAGS -Olimit 4000"
         ac_safe=`echo "machine/builtins.h" | sed 'y%./+-%__p_%'`
 echo $ac_n "checking for machine/builtins.h""... $ac_c" 1>&6
-echo "configure:4164: checking for machine/builtins.h" >&5
+echo "configure:4184: checking for machine/builtins.h" >&5
 if eval "test \"`echo '$''{'ac_cv_header_$ac_safe'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4169 "configure"
+#line 4189 "configure"
 #include "confdefs.h"
 #include <machine/builtins.h>
 EOF
 ac_try="$ac_cpp conftest.$ac_ext >/dev/null 2>conftest.out"
-{ (eval echo configure:4174: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
+{ (eval echo configure:4194: \"$ac_try\") 1>&5; (eval $ac_try) 2>&5; }
 ac_err=`grep -v '^ *+' conftest.out | grep -v "^conftest.${ac_ext}\$"`
 if test -z "$ac_err"; then
   rm -rf conftest*
   eval "ac_cv_header_$ac_safe=yes"
 else
   echo "$ac_err" >&5
   echo "configure: failed program was:" >&5
   cat conftest.$ac_ext >&5
@@ -4673,22 +4693,22 @@ EOF
 
     ;;
    
 esac
 
 if test -z "$SKIP_LIBRARY_CHECKS"; then
 
 echo $ac_n "checking for dlopen""... $ac_c" 1>&6
-echo "configure:4682: checking for dlopen" >&5
+echo "configure:4702: checking for dlopen" >&5
 if eval "test \"`echo '$''{'ac_cv_func_dlopen'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4687 "configure"
+#line 4707 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char dlopen(); below.  */
 #include <assert.h>
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
     builtin and then its argument prototype would still apply.  */
 char dlopen();
@@ -4701,17 +4721,17 @@ int main() {
 #if defined (__stub_dlopen) || defined (__stub___dlopen)
 choke me
 #else
 dlopen();
 #endif
 
 ; return 0; }
 EOF
-if { (eval echo configure:4710: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:4730: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func_dlopen=yes"
 else
   echo "configure: failed program was:" >&5
   cat conftest.$ac_ext >&5
   rm -rf conftest*
   eval "ac_cv_func_dlopen=no"
 fi
@@ -4720,36 +4740,36 @@ fi
 
 if eval "test \"`echo '$ac_cv_func_'dlopen`\" = yes"; then
   echo "$ac_t""yes" 1>&6
   :
 else
   echo "$ac_t""no" 1>&6
 
     echo $ac_n "checking for dlopen in -ldl""... $ac_c" 1>&6
-echo "configure:4729: checking for dlopen in -ldl" >&5
+echo "configure:4749: checking for dlopen in -ldl" >&5
 ac_lib_var=`echo dl'_'dlopen | sed 'y%./+-%__p_%'`
 if eval "test \"`echo '$''{'ac_cv_lib_$ac_lib_var'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   ac_save_LIBS="$LIBS"
 LIBS="-ldl  $LIBS"
 cat > conftest.$ac_ext <<EOF
-#line 4737 "configure"
+#line 4757 "configure"
 #include "confdefs.h"
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
     builtin and then its argument prototype would still apply.  */
 char dlopen();
 
 int main() {
 dlopen()
 ; return 0; }
 EOF
-if { (eval echo configure:4748: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:4768: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=yes"
 else
   echo "configure: failed program was:" >&5
   cat conftest.$ac_ext >&5
   rm -rf conftest*
   eval "ac_cv_lib_$ac_lib_var=no"
 fi
@@ -4767,23 +4787,23 @@ fi
 
 fi
 
 
 
 
 if test $ac_cv_prog_gcc = yes; then
     echo $ac_n "checking whether ${CC-cc} needs -traditional""... $ac_c" 1>&6
-echo "configure:4776: checking whether ${CC-cc} needs -traditional" >&5
+echo "configure:4796: checking whether ${CC-cc} needs -traditional" >&5
 if eval "test \"`echo '$''{'ac_cv_prog_gcc_traditional'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
     ac_pattern="Autoconf.*'x'"
   cat > conftest.$ac_ext <<EOF
-#line 4782 "configure"
+#line 4802 "configure"
 #include "confdefs.h"
 #include <sgtty.h>
 Autoconf TIOCGETP
 EOF
 if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
   egrep "$ac_pattern" >/dev/null 2>&1; then
   rm -rf conftest*
   ac_cv_prog_gcc_traditional=yes
@@ -4791,17 +4811,17 @@ else
   rm -rf conftest*
   ac_cv_prog_gcc_traditional=no
 fi
 rm -f conftest*
 
 
   if test $ac_cv_prog_gcc_traditional = no; then
     cat > conftest.$ac_ext <<EOF
-#line 4800 "configure"
+#line 4820 "configure"
 #include "confdefs.h"
 #include <termio.h>
 Autoconf TCGETA
 EOF
 if (eval "$ac_cpp conftest.$ac_ext") 2>&5 |
   egrep "$ac_pattern" >/dev/null 2>&1; then
   rm -rf conftest*
   ac_cv_prog_gcc_traditional=yes
@@ -4815,22 +4835,22 @@ echo "$ac_t""$ac_cv_prog_gcc_traditional
   if test $ac_cv_prog_gcc_traditional = yes; then
     CC="$CC -traditional"
   fi
 fi
 
 for ac_func in lchown strerror
 do
 echo $ac_n "checking for $ac_func""... $ac_c" 1>&6
-echo "configure:4824: checking for $ac_func" >&5
+echo "configure:4844: checking for $ac_func" >&5
 if eval "test \"`echo '$''{'ac_cv_func_$ac_func'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
   cat > conftest.$ac_ext <<EOF
-#line 4829 "configure"
+#line 4849 "configure"
 #include "confdefs.h"
 /* System header to define __stub macros and hopefully few prototypes,
     which can conflict with char $ac_func(); below.  */
 #include <assert.h>
 /* Override any gcc2 internal prototype to avoid an error.  */
 /* We use char because int might match the return type of a gcc2
     builtin and then its argument prototype would still apply.  */
 char $ac_func();
@@ -4843,17 +4863,17 @@ int main() {
 #if defined (__stub_$ac_func) || defined (__stub___$ac_func)
 choke me
 #else
 $ac_func();
 #endif
 
 ; return 0; }
 EOF
-if { (eval echo configure:4852: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
+if { (eval echo configure:4872: \"$ac_link\") 1>&5; (eval $ac_link) 2>&5; } && test -s conftest${ac_exeext}; then
   rm -rf conftest*
   eval "ac_cv_func_$ac_func=yes"
 else
   echo "configure: failed program was:" >&5
   cat conftest.$ac_ext >&5
   rm -rf conftest*
   eval "ac_cv_func_$ac_func=no"
 fi
@@ -4882,17 +4902,17 @@ if test "${enable_strip+set}" = set; the
 fi
 
 
 case "${target_os}" in
 hpux*)
 if test -z "$GNU_CC"; then
 
     echo $ac_n "checking for +Olit support""... $ac_c" 1>&6
-echo "configure:4891: checking for +Olit support" >&5
+echo "configure:4911: checking for +Olit support" >&5
 if eval "test \"`echo '$''{'ac_cv_hpux_usable_olit_option'+set}'`\" = set"; then
   echo $ac_n "(cached) $ac_c" 1>&6
 else
                   ac_cv_hpux_usable_olit_option=no
         rm -f conftest*
         echo 'int main() { return 0; }' | cat > conftest.c
         ${CC-cc} ${CFLAGS} +Olit=all -o conftest conftest.c > conftest.out 2>&1
         if test $? -eq 0; then
@@ -4916,17 +4936,17 @@ echo "$ac_t""$ac_cv_hpux_usable_olit_opt
 fi
 ;;
 esac
 
 
 
 
 echo $ac_n "checking for pthread_create in -lpthreads""... $ac_c" 1>&6
-echo "configure:4925: checking for pthread_create in -lpthreads" >&5
+echo "configure:4945: checking for pthread_create in -lpthreads" >&5
 echo "
     #include <pthread.h> 
     void *foo(void *v) { return v; } 
     int main() { 
         pthread_t t;
         if (!pthread_create(&t, 0, &foo, 0)) {
             pthread_join(t, 0);
         }
@@ -4938,17 +4958,17 @@ echo "
     rm -f dummy.c dummy${ac_exeext} ;
     if test "$_res" = "0"; then
         echo "$ac_t""yes" 1>&6
         _HAVE_PTHREADS=1 _PTHREAD_LDFLAGS="-lpthreads"
     else
         echo "$ac_t""no" 1>&6
         
 echo $ac_n "checking for pthread_create in -lpthread""... $ac_c" 1>&6
-echo "configure:4947: checking for pthread_create in -lpthread" >&5
+echo "configure:4967: checking for pthread_create in -lpthread" >&5
 echo "
     #include <pthread.h> 
     void *foo(void *v) { return v; } 
     int main() { 
         pthread_t t;
         if (!pthread_create(&t, 0, &foo, 0)) {
             pthread_join(t, 0);
         }
@@ -4960,17 +4980,17 @@ echo "
     rm -f dummy.c dummy${ac_exeext} ;
     if test "$_res" = "0"; then
         echo "$ac_t""yes" 1>&6
         _HAVE_PTHREADS=1 _PTHREAD_LDFLAGS="-lpthread"
     else
         echo "$ac_t""no" 1>&6
         
 echo $ac_n "checking for pthread_create in -lc_r""... $ac_c" 1>&6
-echo "configure:4969: checking for pthread_create in -lc_r" >&5
+echo "configure:4989: checking for pthread_create in -lc_r" >&5
 echo "
     #include <pthread.h> 
     void *foo(void *v) { return v; } 
     int main() { 
         pthread_t t;
         if (!pthread_create(&t, 0, &foo, 0)) {
             pthread_join(t, 0);
         }
@@ -4982,17 +5002,17 @@ echo "
     rm -f dummy.c dummy${ac_exeext} ;
     if test "$_res" = "0"; then
         echo "$ac_t""yes" 1>&6
         _HAVE_PTHREADS=1 _PTHREAD_LDFLAGS="-lc_r"
     else
         echo "$ac_t""no" 1>&6
         
 echo $ac_n "checking for pthread_create in -lc""... $ac_c" 1>&6
-echo "configure:4991: checking for pthread_create in -lc" >&5
+echo "configure:5011: checking for pthread_create in -lc" >&5
 echo "
     #include <pthread.h> 
     void *foo(void *v) { return v; } 
     int main() { 
         pthread_t t;
         if (!pthread_create(&t, 0, &foo, 0)) {
             pthread_join(t, 0);
         }
@@ -5134,17 +5154,17 @@ EOF
     fi
 fi
 
 
 if test -n "$USE_PTHREADS"; then
       rm -f conftest*
    ac_cv_have_dash_pthread=no
    echo $ac_n "checking whether ${CC-cc} accepts -pthread""... $ac_c" 1>&6
-echo "configure:5143: checking whether ${CC-cc} accepts -pthread" >&5
+echo "configure:5163: checking whether ${CC-cc} accepts -pthread" >&5
    echo 'int main() { return 0; }' | cat > conftest.c
    ${CC-cc} -pthread -o conftest conftest.c > conftest.out 2>&1
    if test $? -eq 0; then
 	if test -z "`egrep -i '(unrecognize|unknown)' conftest.out | grep pthread`" && test -z "`egrep -i '(error|incorrect)' conftest.out`" ; then
 	    ac_cv_have_dash_pthread=yes
 		case "$target_os" in
 	    freebsd*)
 # Freebsd doesn't use -pthread for compiles, it uses them for linking
@@ -5157,17 +5177,17 @@ echo "configure:5143: checking whether $
 	fi
     fi
     rm -f conftest*
     echo "$ac_t""$ac_cv_have_dash_pthread" 1>&6
 
 			    ac_cv_have_dash_pthreads=no
     if test "$ac_cv_have_dash_pthread" = "no"; then
 	    echo $ac_n "checking whether ${CC-cc} accepts -pthreads""... $ac_c" 1>&6
-echo "configure:5166: checking whether ${CC-cc} accepts -pthreads" >&5
+echo "configure:5186: checking whether ${CC-cc} accepts -pthreads" >&5
     	echo 'int main() { return 0; }' | cat > conftest.c
 	    ${CC-cc} -pthreads -o conftest conftest.c > conftest.out 2>&1
     	if test $? -eq 0; then
 	    	if test -z "`egrep -i '(unrecognize|unknown)' conftest.out | grep pthreads`" && test -z "`egrep -i '(error|incorrect)' conftest.out`" ; then
 			    ac_cv_have_dash_pthreads=yes
 			    CFLAGS="$CFLAGS -pthreads"
 			    CXXFLAGS="$CXXFLAGS -pthreads"
 		    fi
--- a/configure.in
+++ b/configure.in
@@ -931,16 +931,22 @@ case "$target" in
     PR_MD_CSRCS=freebsd.c
     ;;
 
 *-hpux*)
     AC_DEFINE(XP_UNIX)
     AC_DEFINE(HPUX)
     AC_DEFINE(_HPUX_SOURCE)
     AC_DEFINE(hppa)
+    # OSF1 and HPUX report the POLLHUP event for a socket when the
+    # shutdown(SHUT_WR) operation is called for the remote end, even though
+    # the socket is still writeable. Use select(), instead of poll(), to
+    # workaround this problem.
+    AC_DEFINE(_PR_POLL_WITH_SELECT)
+    AC_DEFINE(_USE_BIG_FDS)
     DLL_SUFFIX=sl
     DSO_LDOPTS='-b +h $(notdir $@)'
     PR_MD_CSRCS=hpux.c
     if test "$OS_TEST" != "ia64"; then
         PR_MD_ASFILES=os_HPUX.s
     fi
     if test -n "$USE_64"; then
         MDCPUCFG_H=_hpux64.cfg
@@ -1493,16 +1499,21 @@ mips-sony-newsos*)
     fi
     ;;
 
 *-osf*)
     SHELL_OVERRIDE="SHELL		= /usr/bin/ksh"
     AC_DEFINE(XP_UNIX)
     AC_DEFINE(OSF1)
     AC_DEFINE(_REENTRANT)
+    # OSF1 and HPUX report the POLLHUP event for a socket when the
+    # shutdown(SHUT_WR) operation is called for the remote end, even though
+    # the socket is still writeable. Use select(), instead of poll(), to
+    # workaround this problem.
+    AC_DEFINE(_PR_POLL_WITH_SELECT)
 
     if echo "$OS_RELEASE" | egrep -c '(V2.0|V3.2)' 2>/dev/null ; then
         USE_NSPR_THREADS=1
     fi
 
     if test -z "$GNU_CC"; then
         CC="$CC -std1 -ieee_with_inexact"
         if test "$OS_RELEASE" != "V2.0"; then
--- a/pr/include/md/_hpux.h
+++ b/pr/include/md/_hpux.h
@@ -41,17 +41,24 @@
 
 #define PR_LINKER_ARCH    "hpux"
 #define _PR_SI_SYSNAME   "HPUX"
 #define _PR_SI_ARCHITECTURE "hppa1.1"
 #define PR_DLL_SUFFIX        ".sl"
 
 #define _PR_VMBASE        0x30000000 
 #define _PR_STACK_VMBASE    0x50000000
-#define _MD_DEFAULT_STACK_SIZE    65536L
+/*
+ * _USE_BIG_FDS increases the size of fd_set from 256 bytes to
+ * about 7500 bytes.  PR_Poll allocates three fd_sets on the
+ * stack, so it is safer to also increase the default thread
+ * stack size.
+ */
+#define _MD_DEFAULT_STACK_SIZE    (2*65536L)
+#define _MD_MINIMUM_STACK_SIZE    (2*65536L)
 #define _MD_MMAP_FLAGS          MAP_PRIVATE
 
 #define NEED_TIME_R
 
 #define HAVE_STACK_GROWING_UP
 #undef	HAVE_WEAK_IO_SYMBOLS
 #undef	HAVE_WEAK_MALLOC_SYMBOLS
 #define	HAVE_DLL
@@ -60,23 +67,16 @@
 #else
 #define USE_HPSHL
 #endif
 #ifndef HAVE_STRERROR
 #define HAVE_STRERROR
 #endif
 #define _PR_POLL_AVAILABLE
 #define _PR_USE_POLL
-/*
- * OSF1 and HPUX report the POLLHUP event for a socket when the
- * shutdown(SHUT_WR) operation is called for the remote end, even though
- * the socket is still writeable. Use select(), instead of poll(), to
- * workaround this problem.
- */
-#define _PR_POLL_WITH_SELECT
 #define _PR_STAT_HAS_ONLY_ST_ATIME
 #define _PR_HAVE_POSIX_SEMAPHORES
 #define PR_HAVE_POSIX_NAMED_SHARED_MEMORY
 #define _PR_ACCEPT_INHERIT_NONBLOCK
 
 #undef _PR_HAVE_ATOMIC_OPS
 
 #if !defined(_PR_PTHREADS)
--- a/pr/include/md/_osf1.h
+++ b/pr/include/md/_osf1.h
@@ -55,23 +55,16 @@
 #define HAVE_DLL
 #define HAVE_BSD_FLOCK
 
 #define NEED_TIME_R
 #define USE_DLFCN
 
 #define _PR_POLL_AVAILABLE
 #define _PR_USE_POLL
-/*
- * OSF1 and HPUX report the POLLHUP event for a socket when the
- * shutdown(SHUT_WR) operation is called for the remote end, even though
- * the socket is still writeable. Use select(), instead of poll(), to
- * workaround this problem.
- */
-#define _PR_POLL_WITH_SELECT
 #define _PR_STAT_HAS_ONLY_ST_ATIME
 #define _PR_HAVE_LARGE_OFF_T
 #define _PR_HAVE_GETIPNODEBYNAME
 #define _PR_HAVE_GETIPNODEBYADDR
 #define _PR_INET6_PROBE
 #ifdef _PR_INET6
 #define _PR_HAVE_INET_NTOP
 #else
--- a/pr/src/pthreads/ptio.c
+++ b/pr/src/pthreads/ptio.c
@@ -35,19 +35,21 @@
 /*
 ** File:   ptio.c
 ** Descritpion:  Implemenation of I/O methods for pthreads
 */
 
 #if defined(_PR_PTHREADS)
 
 #if defined(_PR_POLL_WITH_SELECT)
+#if !(defined(HPUX) && defined(_USE_BIG_FDS))
 /* set fd limit for select(), before including system header files */
 #define FD_SETSIZE (16 * 1024)
 #endif
+#endif
 
 #include <pthread.h>
 #include <string.h>  /* for memset() */
 #include <sys/types.h>
 #include <dirent.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/socket.h>
@@ -3339,21 +3341,16 @@ static const PRIOMethods* PR_GetSocketPo
     return &_pr_socketpollfd_methods;
 }  /* PR_GetSocketPollFdMethods */
 
 PR_IMPLEMENT(PRFileDesc*) PR_AllocFileDesc(
     PRInt32 osfd, const PRIOMethods *methods)
 {
     PRFileDesc *fd = _PR_Getfd();
 
-    /*
-     * Assert that the file descriptor is small enough to fit in the
-     * fd_set passed to select
-     */
-    PR_ASSERT(osfd < FD_SETSIZE);
     if (NULL == fd) goto failed;
 
     fd->methods = methods;
     fd->secret->md.osfd = osfd;
     /* Make fd non-blocking */
     if (osfd > 2)
     {
         /* Don't mess around with stdin, stdout or stderr */
@@ -4032,53 +4029,75 @@ static PRInt32 _pr_poll_with_select(
                         pds[index].fd, PR_NSPR_IO_LAYER);
                     PR_ASSERT(NULL != bottom);  /* what to do about that? */
                     pds[index].out_flags = 0;  /* pre-condition */
                     if ((NULL != bottom)
                     && (_PR_FILEDESC_OPEN == bottom->secret->state))
                     {
                         if (0 == ready)
                         {
+                            PRBool add_to_rd = PR_FALSE;
+                            PRBool add_to_wr = PR_FALSE;
+                            PRBool add_to_ex = PR_FALSE;
+
                             selectfd[index] = bottom->secret->md.osfd;
                             if (in_flags_read & PR_POLL_READ)
                             {
                                 pds[index].out_flags |=
                                     _PR_POLL_READ_SYS_READ;
-								FD_SET(bottom->secret->md.osfd, &rd);
-								rdp = &rd;
+                                add_to_rd = PR_TRUE;
                             }
                             if (in_flags_read & PR_POLL_WRITE)
                             {
                                 pds[index].out_flags |=
                                     _PR_POLL_READ_SYS_WRITE;
-								FD_SET(bottom->secret->md.osfd, &wr);
-								wrp = &wr;
+                                add_to_wr = PR_TRUE;
                             }
                             if (in_flags_write & PR_POLL_READ)
                             {
                                 pds[index].out_flags |=
                                     _PR_POLL_WRITE_SYS_READ;
-								FD_SET(bottom->secret->md.osfd, &rd);
-								rdp = &rd;
+                                add_to_rd = PR_TRUE;
                             }
                             if (in_flags_write & PR_POLL_WRITE)
                             {
                                 pds[index].out_flags |=
                                     _PR_POLL_WRITE_SYS_WRITE;
-								FD_SET(bottom->secret->md.osfd, &wr);
-								wrp = &wr;
+                                add_to_wr = PR_TRUE;
+                            }
+                            if (pds[index].in_flags & PR_POLL_EXCEPT)
+                            {
+                                add_to_ex = PR_TRUE;
                             }
-                            if (pds[index].in_flags & PR_POLL_EXCEPT) {
-								FD_SET(bottom->secret->md.osfd, &ex);
-								exp = &ex;
-							}
-							if ((selectfd[index] > maxfd) &&
-									(pds[index].out_flags ||
-									(pds[index].in_flags & PR_POLL_EXCEPT)))
-								maxfd = selectfd[index];
+                            if ((selectfd[index] > maxfd) &&
+                                    (add_to_rd || add_to_wr || add_to_ex))
+                            {
+                                maxfd = selectfd[index];
+                                /*
+                                 * If maxfd is too large to be used with
+                                 * select, fall back to calling poll.
+                                 */
+                                if (maxfd >= FD_SETSIZE)
+                                    break;
+                            }
+                            if (add_to_rd)
+                            {
+                                FD_SET(bottom->secret->md.osfd, &rd);
+                                rdp = &rd;
+                            }
+                            if (add_to_wr)
+                            {
+                                FD_SET(bottom->secret->md.osfd, &wr);
+                                wrp = &wr;
+                            }
+                            if (add_to_ex)
+                            {
+                                FD_SET(bottom->secret->md.osfd, &ex);
+                                exp = &ex;
+                            }
                         }
                     }
                     else
                     {
                         if (0 == ready)
                         {
                             int i;
                             for (i = 0; i < index; i++)
@@ -4093,17 +4112,17 @@ static PRInt32 _pr_poll_with_select(
             }
             else
             {
                 pds[index].out_flags = 0;
             }
         }
         if (0 == ready)
         {
-			if ((maxfd + 1) > FD_SETSIZE)
+			if (maxfd >= FD_SETSIZE)
 			{
 				/*
 				 * maxfd too large to be used with select, fall back to
 				 * calling poll
 				 */
 				return(_pr_poll_with_poll(pds, npds, timeout));
 			}
             switch (timeout)