From 45913f43e54beacca4feb614bbe80089cec6f490 Mon Sep 17 00:00:00 2001 From: Peter Stephenson Date: Mon, 25 Jul 2011 10:20:09 +0000 Subject: 29561: Allow closing of fd's not recorded by the shell --- Src/exec.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'Src/exec.c') diff --git a/Src/exec.c b/Src/exec.c index f5b59a36e..6320f6a67 100644 --- a/Src/exec.c +++ b/Src/exec.c @@ -2975,17 +2975,16 @@ execcmd(Estate state, int input, int output, int how, int last1) fn->fd1 = (int)getintvalue(v); if (errflag) bad = 1; - else if (fn->fd1 > max_zsh_fd) - bad = 3; - else if (fn->fd1 >= 10 && + else if (fn->fd1 <= max_zsh_fd) { + if (fn->fd1 >= 10 && fdtable[fn->fd1] == FDT_INTERNAL) - bad = 4; + bad = 3; + } } if (bad) { const char *bad_msg[] = { "parameter %s does not contain a file descriptor", "can't close file descriptor from readonly parameter %s", - "file descriptor %d out of range, not closed", "file descriptor %d used by shell, not closed" }; if (bad > 2) @@ -2995,11 +2994,18 @@ execcmd(Estate state, int input, int output, int how, int last1) execerr(); } } + /* + * Note we may attempt to close an fd beyond max_zsh_fd: + * OK as long as we never look in fdtable for it. + */ if (!forked && fn->fd1 < 10 && save[fn->fd1] == -2) save[fn->fd1] = movefd(fn->fd1); if (fn->fd1 < 10) closemn(mfds, fn->fd1); - zclose(fn->fd1); + if (zclose(fn->fd1) < 0) { + zwarn("failed to close file descriptor %d: %e", + fn->fd1, errno); + } break; case REDIR_MERGEIN: case REDIR_MERGEOUT: -- cgit 1.4.1