Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: latest from CVS segfaults when FD ulimit is set too low



We can simplify the code a bit if we make redup() return the target fd
on success.  The attached patch also adds a little more error checking
for redup().  I'll check it in later on if nobody objects.

..wayne..
index 3f47636..ad7eb58 100644
--- a/Src/Modules/socket.c
+++ b/Src/Modules/socket.c
@@ -120,10 +120,7 @@ bin_zsocket(char *nam, char **args, Options ops, UNUSED(int func))
 	}
 
 	if (targetfd) {
-	    if (redup(sfd, targetfd) == -1)
-		sfd = -1;
-	    else
-		sfd = targetfd;
+	    sfd = redup(sfd, targetfd);
 	}
 	else {
 	    /* move the fd since no one will want to read from it */
@@ -205,8 +202,11 @@ bin_zsocket(char *nam, char **args, Options ops, UNUSED(int func))
 	}
 
 	if (targetfd) {
-	    redup(rfd, targetfd);
-	    sfd = targetfd;
+	    sfd = redup(rfd, targetfd);
+	    if (sfd < 0) {
+		zerrnam(nam, "could not duplicate socket fd to %d: %e", targetfd, errno);
+		return 1;
+	    }
 	}
 	else {
 	    sfd = rfd;
@@ -242,8 +242,11 @@ bin_zsocket(char *nam, char **args, Options ops, UNUSED(int func))
 	else
 	{
 	    if (targetfd) {
-		redup(sfd, targetfd);
-		sfd = targetfd;
+		sfd = redup(sfd, targetfd);
+		if (sfd < 0) {
+		    zerrnam(nam, "could not duplicate socket fd to %d: %e", targetfd, errno);
+		    return 1;
+		}
 	    }
 
 	    setiparam("REPLY", sfd);
index 2825cb9..3f92050 100644
--- a/Src/Modules/tcp.c
+++ b/Src/Modules/tcp.c
@@ -446,10 +446,7 @@ bin_ztcp(char *nam, char **args, Options ops, UNUSED(int func))
 	}
 
 	if (targetfd) {
-	    if (redup(sess->fd,targetfd) == -1)
-		sess->fd = -1;
-	    else
-		sess->fd = targetfd;
+	    sess->fd = redup(sess->fd, targetfd);
 	}
 	else {
 	    /* move the fd since no one will want to read from it */
@@ -547,8 +544,11 @@ bin_ztcp(char *nam, char **args, Options ops, UNUSED(int func))
 	}
 
 	if (targetfd) {
-	    redup(rfd, targetfd);
-	    sess->fd = targetfd;
+	    sess->fd = redup(rfd, targetfd);
+	    if (sess->fd < 0) {
+		zerrnam(nam, "could not duplicate socket fd to %d: %e", targetfd, errno);
+		return 1;
+	    }
 	}
 	else {
 	    sess->fd = rfd;
@@ -662,8 +662,11 @@ bin_ztcp(char *nam, char **args, Options ops, UNUSED(int func))
 	else
 	{
 	    if (targetfd) {
-		redup(sess->fd, targetfd);
-		sess->fd = targetfd;
+		sess->fd = redup(sess->fd, targetfd);
+		if (sess->fd < 0) {
+		    zerrnam(nam, "could not duplicate socket fd to %d: %e", targetfd, errno);
+		    return 1;
+		}
 	    }
 
 	    setiparam("REPLY", sess->fd);
index 21a7b43..b807eea 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -1654,14 +1654,14 @@ movefd(int fd)
 
 /*
  * Move fd x to y.  If x == -1, fd y is closed.
- * Return 0 for success, -1 for failure.
+ * Returns y for success, -1 for failure.
  */
 
 /**/
 mod_export int
 redup(int x, int y)
 {
-    int ret = 0;
+    int ret = y;
 
     if(x < 0)
 	zclose(y);


Messages sorted by: Reverse Date, Date, Thread, Author