Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 10 Jun 2009 11:46:45 GMT
From:      Robert Watson <rwatson@FreeBSD.org>
To:        Perforce Change Reviews <perforce@freebsd.org>
Subject:   PERFORCE change 163978 for review
Message-ID:  <200906101146.n5ABkjVt045283@repoman.freebsd.org>

next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=163978

Change 163978 by rwatson@rwatson_freebsd_capabilities on 2009/06/10 11:46:38

	Assymptotically approach correctness.

Affected files ...

.. //depot/projects/trustedbsd/capabilities/src/lib/libcapability/libcapability_host.c#3 edit

Differences ...

==== //depot/projects/trustedbsd/capabilities/src/lib/libcapability/libcapability_host.c#3 (text+ko) ====

@@ -30,7 +30,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  *
- * $P4: //depot/projects/trustedbsd/capabilities/src/lib/libcapability/libcapability_host.c#2 $
+ * $P4: //depot/projects/trustedbsd/capabilities/src/lib/libcapability/libcapability_host.c#3 $
  */
 
 #include <sys/types.h>
@@ -92,15 +92,31 @@
  * number.
  */
 static int
-lch_installfds(int fd_count, int *fds)
+lch_installfds(u_int fd_count, int *fds)
 {
-	int i;
+	u_int i;
+	int highestfd;
+
+	if (fd_count == 0)
+		return (0);
+
+	/*
+	 * Identify the highest source file descriptor we care about so that
+	 * when we play the dup2() rearranging game, we don't overwrite any
+	 * we care about.
+	 */
+	highestfd = fds[0];
+	for (i = 1; i < fd_count; i++) {
+		if (fds[i] > highestfd)
+			highestfd = fds[i];
+	}
+	highestfd++;
 
 	/*
 	 * First, move all our descriptors up the range.
 	 */
 	for (i = 0; i < fd_count; i++) {
-		if (dup2(fds[i], fd_count + i) < 0)
+		if (dup2(fds[i], highestfd + i) < 0)
 			return (-1);
 	}
 
@@ -108,7 +124,7 @@
 	 * Now put them back.
 	 */
 	for (i = 0; i < fd_count; i++) {
-		if (dup2(fd_count + i, fds[i]) < 0)
+		if (dup2(highestfd + i, i) < 0)
 			return (-1);
 	}
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200906101146.n5ABkjVt045283>