about summary refs log tree commit diff
path: root/src/misc/wordexp.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2013-11-22 15:55:58 -0500
committerRich Felker <dalias@aerifal.cx>2013-11-22 15:55:58 -0500
commitcaaf7d443d8f502a209cb5489c45ddcccbebdd34 (patch)
treed7de17f17dc4dbf58f39f953f909787ce7608dbf /src/misc/wordexp.c
parent8253f59eae7bdb8b5a0f5b87212671564882d1f0 (diff)
downloadmusl-caaf7d443d8f502a209cb5489c45ddcccbebdd34.tar.gz
musl-caaf7d443d8f502a209cb5489c45ddcccbebdd34.tar.xz
musl-caaf7d443d8f502a209cb5489c45ddcccbebdd34.zip
fix fd leak and case where fd 1 is already closed in wordexp
Diffstat (limited to 'src/misc/wordexp.c')
-rw-r--r--src/misc/wordexp.c8
1 files changed, 4 insertions, 4 deletions
diff --git a/src/misc/wordexp.c b/src/misc/wordexp.c
index 8f0d42f5..1387b5d2 100644
--- a/src/misc/wordexp.c
+++ b/src/misc/wordexp.c
@@ -8,6 +8,7 @@
 #include <sys/wait.h>
 #include <signal.h>
 #include <errno.h>
+#include <fcntl.h>
 #include "pthread_impl.h"
 
 static char *getword(FILE *f)
@@ -88,7 +89,7 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags)
 		we->we_offs = 0;
 	}
 
-	if (pipe(p) < 0) goto nospace;
+	if (pipe2(p, O_CLOEXEC) < 0) goto nospace;
 	__block_all_sigs(&set);
 	pid = fork();
 	__restore_sigs(&set);
@@ -98,9 +99,8 @@ static int do_wordexp(const char *s, wordexp_t *we, int flags)
 		goto nospace;
 	}
 	if (!pid) {
-		dup2(p[1], 1);
-		close(p[0]);
-		close(p[1]);
+		if (p[1] == 1) fcntl(1, F_SETFD, 0);
+		else dup2(p[1], 1);
 		execl("/bin/sh", "sh", "-c",
 			"eval \"printf %s\\\\\\\\0 x $1 $2\"",
 			"sh", s, redir, (char *)0);