[PATCH] eval: Fix pipe fd leakage in evalbackcmd

Herbert Xu <[email protected]>
Newsgroups org.kernel.vger.dash
Message-ID <[email protected]>
If makejob or forkshell fails in evalbackcmd, the two pipe file
descriptors will be leaked.  Fix this by adding an EXITRESET
handler that frees them.

Signed-off-by: Herbert Xu <[email protected]>
---
 src/eval.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/src/eval.c b/src/eval.c
index d169eb8..1839623 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -81,6 +81,7 @@ int savestatus = -1;		/* exit status of last command outside traps */
 /* Prevent PS4 nesting. */
 MKINIT int inps4;
 
+MKINIT int tpip[2] = { -1 };
 
 #if !defined(__alpha__) || (defined(__GNUC__) && __GNUC__ >= 3)
 STATIC
@@ -127,6 +128,11 @@ EXITRESET {
 	evalskip = 0;
 	loopnest = 0;
 	inps4 = 0;
+
+	if (tpip[0] >= 0) {
+		close(tpip[0]);
+		close(tpip[1]);
+	}
 }
 #endif
 
@@ -624,8 +630,9 @@ evalpipe(union node *n, int flags)
 void
 evalbackcmd(union node *n, struct backcmd *result)
 {
-	int pip[2];
 	struct job *jp;
+	int pip[2];
+	int pid;
 
 	result->fd = -1;
 	result->buf = NULL;
@@ -636,8 +643,12 @@ evalbackcmd(union node *n, struct backcmd *result)
 	}
 
 	sh_pipe(pip, 0);
+	tpip[0] = pip[0];
+	tpip[1] = pip[1];
 	jp = makejob(1);
-	if (forkshell(jp, n, FORK_NOJOB) == 0) {
+	pid = forkshell(jp, n, FORK_NOJOB);
+	tpip[0] = -1;
+	if (pid == 0) {
 		FORCEINTON;
 		close(pip[0]);
 		if (pip[1] != 1) {
-- 
2.39.2

-- 
Email: Herbert Xu <[email protected]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.