diff mbox series

eval: Fix pipe fd leakage in evalbackcmd

Message ID ZlMSPD7YW_o_Fj9N@gondor.apana.org.au (mailing list archive)
State Accepted
Delegated to: Herbert Xu
Headers show
Series eval: Fix pipe fd leakage in evalbackcmd | expand

Commit Message

Herbert Xu May 26, 2024, 10:43 a.m. UTC
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 <herbert@gondor.apana.org.au>
---
 src/eval.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
diff mbox series

Patch

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) {