[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] pselect: reject invalid file descriptors
From: |
Eric Blake |
Subject: |
[PATCH] pselect: reject invalid file descriptors |
Date: |
Tue, 2 Oct 2012 19:21:40 -0600 |
Similar to the recent select fixes.
* doc/posix-functions/pselect.texi (pselect): Document this.
* m4/pselect.m4 (gl_FUNC_PSELECT): Probe for FreeBSD bug.
* lib/pselect.c (rpl_pselect) [!win32]: Work around it.
* modules/pselect (Depends-on): Add dup2.
---
ChangeLog | 6 ++++++
doc/posix-functions/pselect.texi | 4 ++++
lib/pselect.c | 34 ++++++++++++++++++++++++++++++++++
m4/pselect.m4 | 40 +++++++++++++++++++++++++++++++++++++++-
modules/pselect | 1 +
5 files changed, 84 insertions(+), 1 deletion(-)
diff --git a/ChangeLog b/ChangeLog
index 36511c3..b35759f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
2012-10-02 Eric Blake <address@hidden>
+ pselect: reject invalid file descriptors
+ * doc/posix-functions/pselect.texi (pselect): Document this.
+ * m4/pselect.m4 (gl_FUNC_PSELECT): Probe for FreeBSD bug.
+ * lib/pselect.c (rpl_pselect) [!win32]: Work around it.
+ * modules/pselect (Depends-on): Add dup2.
+
select: reject invalid file descriptors
* doc/posix-functions/select.texi (select): Document this.
* m4/select.m4 (gl_FUNC_SELECT): Probe for FreeBSD bug.
diff --git a/doc/posix-functions/pselect.texi b/doc/posix-functions/pselect.texi
index 4c6f093..aa191cc 100644
--- a/doc/posix-functions/pselect.texi
+++ b/doc/posix-functions/pselect.texi
@@ -11,6 +11,10 @@ pselect
@item
This function is missing on some platforms:
OpenBSD 3.8, Minix 3.1.8, AIX 5.1, HP-UX 11.23, IRIX 6.5, OSF/1 5.1, Solaris
9, mingw, MSVC 9, Interix 3.5, BeOS.
address@hidden
+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/pselect.c b/lib/pselect.c
index 4e7a7ed..b544c7c 100644
--- a/lib/pselect.c
+++ b/lib/pselect.c
@@ -33,6 +33,8 @@
pointer parameter stands for no descriptors, an infinite timeout,
or an unaffected signal mask. */
+#if !HAVE_PSELECT
+
int
pselect (int nfds, fd_set *restrict rfds,
fd_set *restrict wfds, fd_set *restrict xfds,
@@ -74,3 +76,35 @@ pselect (int nfds, fd_set *restrict rfds,
return select_result;
}
+
+#else /* HAVE_PSELECT */
+# include <unistd.h>
+# undef pselect
+
+int
+rpl_pselect (int nfds, fd_set *restrict rfds,
+ fd_set *restrict wfds, fd_set *restrict xfds,
+ struct timespec const *restrict timeout,
+ sigset_t const *restrict sigmask)
+{
+ 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;
+ }
+
+ return pselect (nfds, rfds, wfds, xfds, timeout, sigmask);
+}
+
+#endif
diff --git a/m4/pselect.m4 b/m4/pselect.m4
index 97bf12c..5edacd2 100644
--- a/m4/pselect.m4
+++ b/m4/pselect.m4
@@ -1,4 +1,4 @@
-# pselect.m4
+# pselect.m4 serial 2
dnl Copyright (C) 2011-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,
@@ -23,6 +23,44 @@ AC_DEFUN([gl_FUNC_PSELECT],
return !p;]])],
[gl_cv_sig_pselect=yes],
[gl_cv_sig_pselect=no])])
+
+ dnl On FreeBSD 8.2, pselect() doesn't always reject bad fds.
+ AC_CACHE_CHECK([whether pselect detects invalid fds],
+ [gl_cv_func_pselect_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 timespec timeout;
+ timeout.tv_sec = 0;
+ timeout.tv_nsec = 5000;
+ return pselect (17, &set, NULL, NULL, &timeout, NULL) != -1 || errno !=
EBADF;
+]])], [gl_cv_func_pselect_detects_ebadf=yes],
+ [gl_cv_func_pselect_detects_ebadf=no],
+ [
+ case "$host_os" in
+ # Guess yes on glibc systems.
+ *-gnu*) gl_cv_func_pselect_detects_ebadf="guessing yes" ;;
+ # If we don't know, assume the worst.
+ *) gl_cv_func_pselect_detects_ebadf="guessing no" ;;
+ esac
+ ])
+ ])
+ case $gl_cv_func_pselect_detects_ebadf in
+ *yes) ;;
+ *) REPLACE_PSELECT=1 ;;
+ esac
fi
if test $ac_cv_func_pselect = no || test $gl_cv_sig_pselect = no; then
diff --git a/modules/pselect b/modules/pselect
index 1ca08ed..9db819c 100644
--- a/modules/pselect
+++ b/modules/pselect
@@ -9,6 +9,7 @@ Depends-on:
sys_select
pthread_sigmask [test $HAVE_PSELECT = 0 || test $REPLACE_PSELECT = 1]
select [test $HAVE_PSELECT = 0 || test $REPLACE_PSELECT = 1]
+dup2 [test $REPLACE_PSELECT = 1]
configure.ac:
gl_FUNC_PSELECT
--
1.7.11.4