1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

Fix typeset attributes -a, -A, -l, -u leaking out of subshells

If an array or upper/lowercase variable was declared with a null
initial value within a virtual/non-forked subshell, like:
	( typeset -a foo; ... )
	( typeset -A foo; ... )
	( typeset -l foo; ... )
	( typeset -u foo; ... )
then the type declaration leaked out of the subshell into the
parent shell environment, though without any values that may
subsequently have been assigned.

src/cmd/ksh93/bltins/typeset.c: setall():
- When deciding whether to create a virtual subshell scope for a
  variable, use sh_assignok(), which was actually designed for the
  purpose, instead of _nv_unset(). This allows getting rid of a
  tangled mess of special-casing that never worked quite right.

src/cmd/ksh93/tests/arrays.sh:
- Add regression tests checking that array declarations don't leak
  out of virtual subshells.

src/cmd/ksh93/tests/attributes.sh:
- Add regression tests for combining the 'export' and 'readonly'
  attributes with every other possible typeset attribute on unset
  variables. This also includes a subshell leak test for each one.

Fixes: https://github.com/ksh93/ksh/issues/88
This commit is contained in:
Martijn Dekker 2020-07-25 16:39:12 +01:00
parent 1bc2c74c74
commit a2f13c19f2
4 changed files with 99 additions and 5 deletions

View file

@ -180,6 +180,12 @@ then err_exit 'arithmetic expressions in typeset not working'
fi
unset foo
typeset foo=bar
typeset -a foo
if [[ ${foo[0]} != bar ]]
then err_exit 'initial value not preserved when typecast to indexed array'
fi
unset foo
typeset foo=bar
typeset -A foo
if [[ ${foo[0]} != bar ]]
then err_exit 'initial value not preserved when typecast to associative'
@ -674,5 +680,32 @@ actual=$("$SHELL" -c 'A[0]="'\''" B[0]=aa C[0]=aa; typeset -a') \
[[ $actual == "$expect" ]] || err_exit 'Incorrect output when listing indexed array' \
"(expected $(printf %q "$expect"), got $(printf %q "$actual"))"
# ======
# Arrays in virtual/non-forked subshells
unset foo
(typeset -a foo)
[[ -n ${ typeset -p foo; } ]] && err_exit 'Null indexed array leaks out of subshell'
unset foo
(typeset -a foo=(1 2 3))
[[ -n ${ typeset -p foo; } ]] && err_exit 'Indexed array with init value leaks out of subshell'
unset foo
(typeset -a foo; foo=(1 2 3))
[[ -n ${ typeset -p foo; } ]] && err_exit 'Indexed array leaks out of subshell'
unset foo
(typeset -A foo)
[[ -n ${ typeset -p foo; } ]] && err_exit 'Null associative array leaks out of subshell'
unset foo
(typeset -A foo=([bar]=baz [lorem]=ipsum))
[[ -n ${ typeset -p foo; } ]] && err_exit 'Associative array with init value leaks out of subshell'
unset foo
(typeset -A foo; foo=([bar]=baz [lorem]=ipsum))
[[ -n ${ typeset -p foo; } ]] && err_exit 'Associative array leaks out of subshell'
# ======
exit $((Errors<125?Errors:125))