Skip to content

Commit 503a596

Browse files
authored
Merge pull request ksh-community#16 from JohnoKing/fix-optimized-variables
Remove a buggy optimization for variables in subshells Fixes ksh-community#15
2 parents ef1621c + 3d38270 commit 503a596

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ Any uppercase BUG_* names are modernish shell bug IDs.
77

88
- The 'source' alias has been converted into a regular built-in command.
99

10+
- Functions that set variables in a virtual subshell will no longer affect
11+
variables of the same name outside of the virtual subshell's environment.
12+
1013
2020-06-14:
1114

1215
- 'read -S' is now able to correctly handle strings with double quotes

src/cmd/ksh93/sh/subshell.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,6 @@ Namval_t *sh_assignok(register Namval_t *np,int add)
251251
/* don't bother with this */
252252
if(!sp->shpwd || np==SH_LEVELNOD || np==L_ARGNOD || np==SH_SUBSCRNOD || np==SH_NAMENOD)
253253
return(np);
254-
/* don't bother to save if in newer scope */
255-
if(sp->var!=shp->var_tree && sp->var!=shp->var_base && shp->last_root==shp->var_tree)
256-
return(np);
257254
if((ap=nv_arrayptr(np)) && (mp=nv_opensub(np)))
258255
{
259256
shp->last_root = ap->table;

src/cmd/ksh93/tests/subshell.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,5 +726,18 @@ check_hash_table()
726726
(hash cat)
727727
[[ $(hash) == "chmod=$(whence -p chmod)" ]] || err_exit $'changes to a subshell\'s hash table affect the parent shell'
728728

729+
# ======
730+
# Variables set in functions inside of a virtual subshell should not affect the
731+
# outside environment. This regression test must be run from the disk.
732+
testvars=$tmp/testvars.sh
733+
cat >| "$testvars" << 'EOF'
734+
c=0
735+
function set_ac { a=1; c=1; }
736+
function set_abc { ( set_ac ; b=1 ) }
737+
set_abc
738+
echo "a=$a b=$b c=$c"
739+
EOF
740+
v=$($SHELL $testvars) && [[ "$v" == "a= b= c=0" ]] || err_exit 'variables set in subshells are not confined to the subshell'
741+
729742
# ======
730743
exit $((Errors<125?Errors:125))

0 commit comments

Comments
 (0)