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

alias: Avoid unnecessary forks (re: ec888867) (#417)

The code used to fork subshells when creating/changing aliases will
always fork, even when the alias tree isn't changed:
   $ echo $(unalias --man 2> /dev/null; echo $$ ${.sh.pid})
   375097 375107
   $ alias foo=bar; echo $(alias -p foo; echo $$ ${.sh.pid})
   alias foo=bar 375097 375110
This is a bit inefficient, so this commit avoids forking a subshell
unless at least one change is made to the alias table.

src/cmd/ksh93/bltins/typeset.c:
- b_alias(), b_unalias(): Remove sh_subfork() calls.
- setall(): When creating an alias (name contains '='), fork a
  virtual subshell before calling nv_open() to add the node.
- unall():
  - When unsetting all aliases (-a), fork subshell before dtclear().
  - When unsetting one alias, fork subshell before nv_delete().
  - Move sh_pushcontext() and sh_popcontext() expansions so that
    sh_subfork() is not in between them, as that would cause
    program flow corruption or a crash.

Co-authored-by: Martijn Dekker <martijn@inlv.org>
This commit is contained in:
Johnothan King 2022-01-12 17:55:36 -08:00 committed by Martijn Dekker
parent b509e92241
commit 40b2c3c3d4
2 changed files with 35 additions and 10 deletions

View file

@ -275,5 +275,23 @@ ret=$?
got=$?
((got > 0)) || err_exit "Exit status is zero when alias is passed 256 non-existent aliases"
# ======
# https://github.com/ksh93/ksh/pull/417
alias foo=bar
(unalias -a)
alias foo >/dev/null 2>&1 || err_exit "unalias -a leaked out of subshell"
unalias foo
(alias foo=bar)
alias foo >/dev/null 2>&1 && err_exit "alias leaked out of subshell"
alias foo=bar
exp="0 $$"
got=$(alias foo='echo "$? $$"'; eval foo)
[[ $got == "$exp" ]] || err_exit "alias in subshell: expected $(printf %q "$exp"), got $(printf %q "$got")"
got=$(unalias foo; echo "$? $$")
[[ $got == "$exp" ]] || err_exit "unalias in subshell: expected $(printf %q "$exp"), got $(printf %q "$got")"
unalias foo
# ======
exit $((Errors<125?Errors:125))