1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-24 23:14:14 +00:00
cde/src/cmd/ksh93/tests
Martijn Dekker 911d6b066f Fix subshell scoping of changes in shared command substitution
A ${ shared-state command substitution; } (internally called
subshare) is documented to share its state with the parent shell
environment, so all changes made within the command substitution
survive outside of it. However, when it is run within a
virtual/non-forked subshell, variables that are not already local
to that subshell will leak out of it into the grandparent state.
Reproducer:

	$ ksh -c '( v=${ bug=BAD; } ); echo "$bug"'
	BAD

If the variable pre-exists in the subshell, the bug does not occur:

	$ ksh -c '( bug=BAD1; v=${ bug=BAD2; } ); echo "$bug"'
	(empty line, as expected)

The problem is that the sh_assignok() function, which is
responsible for variable scoping in virtual subshells, does not
ever bother to create a virtual subshell scope for a subshare.
That is an error if a subshare's parent (or higher-up ancestor)
environment is a virtual subshell, because a scope needs to be
created in that parent environment if none exists.

To make this bugfix possible, first we need to get something out of
the way. nv_restore() temporarily sets the subshell's pointer to
the preesnt working directory, shpwd, to null. This causes
sh_assignok() to assume that the subshell is a subshare (because
subshares don't store their own PWD) and refuse to create a scope.
However, nv_restore() sets it to null for a different purpose: to
temporarily disable scoping for *all* virtual subshells, making
restoring possible. This is a good illustration of why it's often
not a good idea to use the same variable for unrelated purposes.

src/cmd/ksh93/sh/subshell.c:
- Add a global static subshell_noscope flag variable to replace the
  misuse of sh.shpwd described above.
- sh_assignok():
  . Check subshell_noscope instead of shpwd to see if scope
    creation is disabled. This makes it possible to distinguish
    between restoring scope and handling subshares.
  . If the current environment is a subshare that is in a virtual
    subshell, create a scope in the parent subshell. This is done
    by temporarily making the parent virtual subshell the current
    subshell (by setting the global subshell_data pointer to it)
    and calling sh_assignok() again, recursively.
- nv_restore(): To disable subshell scope creation while restoring,
  set subshell_noscope instead of saving and unsetting sh.shpwd.

src/cmd/ksh93/tests/subshell.sh:
- Add tests. I like tests. Tests are good.

Fixes: https://github.com/ksh93/ksh/issues/143
2021-02-17 15:33:48 +00:00
..
alias.sh Fix a crash on unsetting preset alias (re: ddaa145b) (#133) 2020-09-18 11:17:20 +01:00
append.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
arith.sh Produce IEEE compliant output from pow() despite platform deviations 2021-02-12 13:23:16 +00:00
arrays.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
arrays2.sh Fix disabling SHOPT_FIXEDARRAY (re: 2182ecfa) 2021-02-10 04:48:56 +00:00
attributes.sh tests/attributes.sh: fix spurious fail if any env var contains 'foo' 2021-02-12 12:45:47 +00:00
basic.sh move exit/return tests from basic.sh to return.sh (re: 092b90da) 2021-02-14 06:32:57 +00:00
bracket.sh github: Re-disable Mac CI runner (re: 5c389035) 2021-02-13 06:58:30 +00:00
builtins.sh tests/builtins.sh: fix fail due to translated system messages 2021-02-13 23:27:43 +00:00
case.sh More 'case' regression tests (re: e37aa358) 2021-02-16 13:36:50 +00:00
comvar.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
comvario.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
coprocess.sh regress test tweaks 2020-10-03 00:32:32 +02:00
cubetype.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
enum.sh Correctly block invalid values for arrays of an enum type 2021-02-01 16:57:43 +00:00
exit.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
expand.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
functions.sh sleep: guarantee sleeping specified time at minimum (#174) 2021-02-14 07:27:04 +00:00
glob.sh Fix compile/regress fails on compiling without SHOPT_* options 2021-02-08 22:02:45 +00:00
grep.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
heredoc.sh fix typos: descritor -> descriptor 2020-10-05 18:39:49 +02:00
io.sh Fix miscellaneous typos 2021-02-16 16:45:06 +00:00
jobs.sh test/jobs.sh: use slightly more widely supported ps -o format 2021-02-15 15:41:31 +00:00
leaks.sh Implement leak detection on UnixWare (#172) 2021-02-13 00:52:54 +00:00
locale.sh Fix compile/regress fails on compiling without SHOPT_* options 2021-02-08 22:02:45 +00:00
math.sh Fix compile/regress fails on compiling without SHOPT_* options 2021-02-08 22:02:45 +00:00
nameref.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
namespace.sh Fix compile/regress fails on compiling without SHOPT_* options 2021-02-08 22:02:45 +00:00
options.sh tests/options.sh: add forgotten SHOPT_BRACEPAT check (re: af5f7acf) 2021-02-15 01:57:17 +00:00
path.sh command -x: tweak args list size detection (re: 9ddb45b1) 2021-02-13 00:08:33 +00:00
pointtype.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
pty.sh Fix emacs backslash escaping behavior (#179) 2021-02-17 14:29:12 +00:00
quoting.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
quoting2.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
readcsv.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
recttype.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
restricted.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
return.sh move exit/return tests from basic.sh to return.sh (re: 092b90da) 2021-02-14 06:32:57 +00:00
select.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
shtests shtests: Stop requiring a tty 2021-02-13 05:55:27 +00:00
sigchld.sh tests/sigchild.sh: increase a sleep to prevent very rare intermittent fail 2020-09-18 20:06:34 +02:00
signal.sh Fix signal/trap behaviour in ksh functions (rhbz#1454804) 2020-09-29 03:16:39 +02:00
statics.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
subshell.sh Fix subshell scoping of changes in shared command substitution 2021-02-17 15:33:48 +00:00
substring.sh Fix compile/regress fails on compiling without SHOPT_* options 2021-02-08 22:02:45 +00:00
tilde.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
timetype.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
treemove.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
types.sh typeset: add error msgs for incompatible options; improve usage msg 2021-01-21 09:36:10 +00:00
variables.sh sleep: guarantee sleeping specified time at minimum (#174) 2021-02-14 07:27:04 +00:00
vartree1.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00
vartree2.sh shtests: cd to each test set's temp dir before running 2020-09-02 06:02:40 +01:00