From: Eric Blake Date: Tue, 2 Oct 2012 21:29:13 +0000 (-0600) Subject: select: reject invalid file descriptors X-Git-Tag: v0.1~406 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=9500a55f083bb8e55ee938e4532ae6baea702e6b;p=gnulib.git select: reject invalid file descriptors POSIX requires invalid file descriptors to be detected rather than silently ignored. FreeBSD 8.2 detects if fd 0 has been closed and appears in a set while fd 1 is still open, but mistakenly optimizes and refuses to check any fds in the set beyond the maximum open fd. * m4/select.m4 (gl_FUNC_SELECT): Probe for FreeBSD bug. * lib/select.c (rpl_select) [!win32]: Work around it. * modules/select (Depends-on): Add dup2. * doc/posix-functions/select.texi (select): Document this. --- diff --git a/ChangeLog b/ChangeLog index 38a0f3fa6e..94447ef4df 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2012-10-02 Eric Blake + select: reject invalid file descriptors + * m4/select.m4 (gl_FUNC_SELECT): Probe for FreeBSD bug. + * lib/select.c (rpl_select) [!win32]: Work around it. + * modules/select (Depends-on): Add dup2. + * doc/posix-functions/select.texi (select): Document this. + select: enhance test * tests/test-select.h (do_select_bad_nfd_nowait, test_bad_nfd): New functions. diff --git a/doc/posix-functions/select.texi b/doc/posix-functions/select.texi index c13b30f55a..26fb2022ad 100644 --- a/doc/posix-functions/select.texi +++ b/doc/posix-functions/select.texi @@ -18,6 +18,10 @@ placed in @code{errno}, and @code{WSAGetLastError} must be used instead. @item This function fails when the @code{nfds} argument is 0 on some platforms: Interix 3.5. +@item +On some platforms, this function fails to detect invalid fds with +EBADF, but only if they lie beyond the current maximum open fd: +FreeBSD 8.2. @end itemize Portability problems not fixed by Gnulib: diff --git a/lib/select.c b/lib/select.c index 4db09a353d..1ff6652c65 100644 --- a/lib/select.c +++ b/lib/select.c @@ -507,6 +507,8 @@ restart: #include #include /* NULL */ +#include +#include #undef select @@ -514,6 +516,23 @@ int rpl_select (int nfds, fd_set *rfds, fd_set *wfds, fd_set *xfds, struct timeval *timeout) { + int i; + + /* FreeBSD 8.2 has a bug: it does not always detect invalid fds. */ + if (nfds < 0 || nfds > FD_SETSIZE) + { + errno = EINVAL; + return -1; + } + for (i = 0; i < nfds; i++) + { + if (((rfds && FD_ISSET (i, rfds)) + || (wfds && FD_ISSET (i, wfds)) + || (xfds && FD_ISSET (i, xfds))) + && dup2 (i, i) != i) + return -1; + } + /* Interix 3.5 has a bug: it does not support nfds == 0. */ if (nfds == 0) { diff --git a/m4/select.m4 b/m4/select.m4 index 037b3d3cfc..d135a39db9 100644 --- a/m4/select.m4 +++ b/m4/select.m4 @@ -1,4 +1,4 @@ -# select.m4 serial 6 +# select.m4 serial 7 dnl Copyright (C) 2009-2012 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, @@ -46,6 +46,44 @@ changequote([,])dnl *yes) ;; *) REPLACE_SELECT=1 ;; esac + + dnl On FreeBSD 8.2, select() doesn't always reject bad fds. + AC_CACHE_CHECK([whether select detects invalid fds], + [gl_cv_func_select_detects_ebadf], + [ + AC_RUN_IFELSE([AC_LANG_PROGRAM([[ +#include +#include +#if HAVE_SYS_SELECT_H +# include +#endif +#include +#include +]],[[ + fd_set set; + dup2(0, 16); + FD_ZERO(&set); + FD_SET(16, &set); + close(16); + struct timeval timeout; + timeout.tv_sec = 0; + timeout.tv_usec = 5; + return select (17, &set, NULL, NULL, &timeout) != -1 || errno != EBADF; +]])], [gl_cv_func_select_detects_ebadf=yes], + [gl_cv_func_select_detects_ebadf=no], + [ + case "$host_os" in + # Guess yes on glibc systems. + *-gnu*) gl_cv_func_select_detects_ebadf="guessing yes" ;; + # If we don't know, assume the worst. + *) gl_cv_func_select_detects_ebadf="guessing no" ;; + esac + ]) + ]) + case $gl_cv_func_select_detects_ebadf in + *yes) ;; + *) REPLACE_SELECT=1 ;; + esac fi dnl Determine the needed libraries. diff --git a/modules/select b/modules/select index ab8ce7e01f..28c89ee9c1 100644 --- a/modules/select +++ b/modules/select @@ -8,6 +8,7 @@ m4/select.m4 Depends-on: sys_select alloca [test $REPLACE_SELECT = 1] +dup2 [test $REPLACE_SELECT = 1] sockets [test $REPLACE_SELECT = 1] sys_time [test $REPLACE_SELECT = 1] msvc-nothrow [test $REPLACE_SELECT = 1]