[PATCH] expand: Fix here-document file descriptor leak

Herbert Xu <[email protected]>
Newsgroups org.kernel.vger.dash
Message-ID <[email protected]>
Swap the order of here-document expansion and pipe creation as
otherwise the pipe file descriptors will become accessible in the
expanded text.

Fixes: f4ee8c859c3d ("[EXPAND] Expand here-documents in the...")
Signed-off-by: Herbert Xu <[email protected]>
---
 src/redir.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/redir.c b/src/redir.c
index d74602c..bcc81b4 100644
--- a/src/redir.c
+++ b/src/redir.c
@@ -335,15 +335,15 @@ openhere(union node *redir)
 	int pip[2];
 	size_t len = 0;
 
-	if (pipe(pip) < 0)
-		sh_error("Pipe call failed");
-
 	p = redir->nhere.doc->narg.text;
 	if (redir->type == NXHERE) {
 		expandarg(redir->nhere.doc, NULL, EXP_QUOTED);
 		p = stackblock();
 	}
 
+	if (pipe(pip) < 0)
+		sh_error("Pipe call failed");
+
 	len = strlen(p);
 	if (len <= PIPESIZE) {
 		xwrite(pip[1], p, len);
-- 
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.