From faa02346a8b23ff0c6694b616c92e93b8e76cd8b Mon Sep 17 00:00:00 2001 From: Peter Stephenson Date: Wed, 26 Oct 2011 18:48:13 +0000 Subject: 29844, 29845: remove bogus error on closing fd's 0 to 9; update test --- ChangeLog | 7 ++++++- Src/exec.c | 14 ++++++++++++-- Test/A04redirect.ztst | 7 +++++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7cc2d1329..529d25293 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2011-10-26 Peter Stephenson + + * 29844, 29845: Src/exec.c, Test/A04redirect.ztst: remove bogus + error on closing fd's 0 to 9; update test. + 2011-10-26 Peter Stephenson * 29865: Src/Modules/pcre.mdd: don't compile if no pcre-config. @@ -15506,5 +15511,5 @@ ***************************************************** * This is used by the shell to define $ZSH_PATCHLEVEL -* $Revision: 1.5486 $ +* $Revision: 1.5487 $ ***************************************************** diff --git a/Src/exec.c b/Src/exec.c index 3d0731b3e..2c644e6b7 100644 --- a/Src/exec.c +++ b/Src/exec.c @@ -2912,6 +2912,7 @@ execcmd(Estate state, int input, int output, int how, int last1) } addfd(forked, save, mfds, fn->fd1, fn->fd2, 1, fn->varid); } else { + int closed; if (fn->type != REDIR_HERESTR && xpandredir(fn, redir)) continue; if (errflag) { @@ -3002,11 +3003,20 @@ execcmd(Estate state, int input, int output, int how, int last1) * 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) + closed = 0; + if (!forked && fn->fd1 < 10 && save[fn->fd1] == -2) { save[fn->fd1] = movefd(fn->fd1); + if (save[fn->fd1] >= 0) { + /* + * The original fd is now closed, we don't need + * to do it below. + */ + closed = 1; + } + } if (fn->fd1 < 10) closemn(mfds, fn->fd1); - if (zclose(fn->fd1) < 0) { + if (!closed && zclose(fn->fd1) < 0) { zwarn("failed to close file descriptor %d: %e", fn->fd1, errno); } diff --git a/Test/A04redirect.ztst b/Test/A04redirect.ztst index c2321a571..e58102664 100644 --- a/Test/A04redirect.ztst +++ b/Test/A04redirect.ztst @@ -156,11 +156,14 @@ read foo <&-) 1:'<&-' redirection ?(eval):1: failed to close file descriptor 3: bad file descriptor -?(eval):2: failed to close file descriptor 0: bad file descriptor print foo >&- 0:'>&-' redirection -?(eval):1: failed to close file descriptor 1: bad file descriptor + + (exec >&- + print foo) +0:'>&-' with attempt to use closed fd +?(eval):2: write error: bad file descriptor fn() { local foo; read foo; print $foo; } coproc fn -- cgit 1.4.1