Mercurial > hg > octave-nkf > gnulib-hg
changeset 17123:e71d6495bb31
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.
author | Eric Blake <eblake@redhat.com> |
---|---|
date | Tue, 02 Oct 2012 15:29:13 -0600 |
parents | 134fbc73d583 |
children | efd310032e74 |
files | ChangeLog doc/posix-functions/select.texi lib/select.c m4/select.m4 modules/select |
diffstat | 5 files changed, 69 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2012-10-02 Eric Blake <eblake@redhat.com> + 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.
--- a/doc/posix-functions/select.texi +++ b/doc/posix-functions/select.texi @@ -18,6 +18,10 @@ @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:
--- a/lib/select.c +++ b/lib/select.c @@ -507,6 +507,8 @@ #include <sys/select.h> #include <stddef.h> /* NULL */ +#include <errno.h> +#include <unistd.h> #undef select @@ -514,6 +516,23 @@ 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) {
--- 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 @@ *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 <sys/types.h> +#include <sys/time.h> +#if HAVE_SYS_SELECT_H +# include <sys/select.h> +#endif +#include <unistd.h> +#include <errno.h> +]],[[ + 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.