1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-13 11:42:21 +00:00

Fix erroneous fork after 'readonly PATH' in subshell (re: 102868f8)

After making PATH readonly in a virtual subshell (without otherwise
changing it, so the subshell is never forked), then the main shell
would erroneously fork into a background process immediately after
leaving the virtual subshell. This was caused by a bug in the
forking workaround that prevents changes in PATH in a virtual
subshell from clearing the parent shell's hash table.

src/cmd/ksh93/sh/name.c: nv_putval():
- If we're either setting or restoring PATH, do an additional check
  for the NV_RDONLY flag, which means the function was told to
  ignore the variable's readonly state. It is told to ignore that
  when restoring the parent shell state after exiting a virtual
  subshell. If we don't fork then, we don't fork the parent shell.

src/cmd/ksh93/tests/subshell.sh:
- Add regression test verifying that no forking happens when making
  PATH readonly in a subshell.

Fixes #30.
This commit is contained in:
Martijn Dekker 2020-06-20 23:39:42 +02:00
parent bd3e2a8001
commit 9d428f8f5e
2 changed files with 18 additions and 1 deletions

View file

@ -1609,8 +1609,11 @@ void nv_putval(register Namval_t *np, const char *string, int flags)
/*
* Resetting the PATH in a non-forking subshell will reset the parent shell's
* hash table, so work around the problem by forking before sh_assignok
* -- however, don't do this if we were told to ignore the variable's readonly state (i.e.
* if the NV_RDONLY flag is set), because that means we're restoring the parent shell state
* after exiting a virtual subshell, and we would end up forking the parent shell instead.
*/
if(shp->subshell && !shp->subshare && np == PATHNOD)
if(np == PATHNOD && !(flags & NV_RDONLY) && shp->subshell && !shp->subshare)
sh_subfork();
/*

View file

@ -750,5 +750,19 @@ echo "a=$a b=$b c=$c"
EOF
v=$($SHELL $testvars) && [[ "$v" == "a= b= c=0" ]] || err_exit 'variables set in subshells are not confined to the subshell'
# ======
# Setting PATH in virtual subshell should trigger a fork; restoring PATH after leaving virtual subshell should not.
# TODO: it would be really nice to have a ${.sh.pid} for this sort of test (like $BASHPID on bash)...
SHELL=$SHELL "$SHELL" -c '
(
"$SHELL" -c "echo DEBUG \$PPID"
readonly PATH
"$SHELL" -c "echo DEBUG \$PPID"
)
"$SHELL" -c "echo DEBUG \$PPID"
: extra command to disable "-c" exec optimization
' | awk '/^DEBUG/ { pid[NR] = $2; } END { exit !(pid[1] == pid[2] && pid[2] == pid[3]); }' \
|| err_exit "setting PATH to readonly in subshell triggers an erroneous fork"
# ======
exit $((Errors<125?Errors:125))