From 7b59fb805c956a9ae9c4ba074f6230c992a9f93d Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Fri, 5 Feb 2021 05:38:38 +0000 Subject: [PATCH] Fix 'redirect {var}>file' in subshell (re: 7b82c338) Permanent redirections of that form broke in subshells when used with the 'redirect' command, because I had overlooked one instance where the new 'redirect' builtin needs to match the behaviour of the 'exec' builtin. src/cmd/ksh93/tests/io.sh: sh_exec(): - Do not restore file descriptors in (virtual) subshells for 'redirect' just as this isn't done for 'exec'. src/cmd/ksh93/tests/io.sh: - Add regression test for this bug. - Complete the test for f9427909 which I committed prematurely. Fixes: https://github.com/ksh93/ksh/issues/167 --- src/cmd/ksh93/sh/xec.c | 4 ++-- src/cmd/ksh93/tests/io.sh | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/cmd/ksh93/sh/xec.c b/src/cmd/ksh93/sh/xec.c index 8735a6b2c..179d71d30 100644 --- a/src/cmd/ksh93/sh/xec.c +++ b/src/cmd/ksh93/sh/xec.c @@ -1401,8 +1401,8 @@ int sh_exec(register const Shnode_t *t, int flags) sh_unscope(shp); bp->ptr = (void*)save_ptr; bp->data = (void*)save_data; - /* don't restore for subshell exec */ - if((shp->topfd>topfd) && !(shp->subshell && np==SYSEXEC)) + /* don't restore for 'exec' or 'redirect' in subshell */ + if((shp->topfd>topfd) && !(shp->subshell && (np==SYSEXEC || np==SYSREDIR))) sh_iorestore(shp,topfd,jmpval); shp->redir0 = 0; diff --git a/src/cmd/ksh93/tests/io.sh b/src/cmd/ksh93/tests/io.sh index 01fc8d4bc..0b0052653 100755 --- a/src/cmd/ksh93/tests/io.sh +++ b/src/cmd/ksh93/tests/io.sh @@ -698,8 +698,13 @@ got=$(export tmp; "$SHELL" -ec \ # ====== # Redirections of the form {varname}>file stopped working if brace expansion was turned off -redirect {v}>$tmp/v.out; echo ok >&$v -[[ $(<$tmp/v.out) == ok ]] || err_exit '{varname}>file not working with brace expansion turned off' +set +B +{ redirect {v}>$tmp/v.out && echo ok >&$v; } 2>/dev/null +set -B +[[ -r $tmp/v.out && $(<$tmp/v.out) == ok ]] || err_exit '{varname}>file not working with brace expansion turned off' +# ...and they didn't work in subshells: https://github.com/ksh93/ksh/issues/167 +(redirect {v}>$tmp/v.out; echo ok2 >&$v) 2>/dev/null +[[ -r $tmp/v.out && $(<$tmp/v.out) == ok2 ]] || err_exit 'redirect {varname}>file not working in a subshell' # ====== exit $((Errors<125?Errors:125))