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

Restore namespace's parent scope when exiting due to error

Reproducer:

    $ namespace test { x=123; typeset -g x=456; }
    $ echo $x ${.test.x}
    456 123
    $ namespace test { typeset -Q; }
    arch/darwin.i386-64/bin/ksh: typeset: -Q: unknown option
    [usage message snipped for brevity]
    $ echo $x ${.test.x}
    123 123	            <== expected: 123 456
    $ x=789
    $ echo $x ${.test.x}
    789 789                 <== expected: 789 456
    $ # look at that, we never left the namespace...

When prefixing the erroneous 'typeset' with 'command', the problem
does not occur. 'command' disables the properties of special
built-ins such as exit on error. So, when a special built-in exits
on error, the parent scope is not properly resotred.

This bug exists in every ksh93 version with SHOPT_NAMESPACE so far.

src/cmd/ksh93/sh/xec.c: sh_exec():
- Before entering a namespace, use sh_pushcontext and sigsetjmp to
  make sure we return here if sh_exit() is called, e.g. when a
  special builtin throws an error, to ensure the parent scope
  (oldnspace) is restored.

Thanks to @hyenias for making me aware of this bug.
Discussion: https://github.com/ksh93/ksh/issues/479#issuecomment-1140468965
This commit is contained in:
Martijn Dekker 2022-05-29 22:50:24 +01:00
parent 8f14514661
commit f73b8617dd
4 changed files with 28 additions and 2 deletions

View file

@ -130,5 +130,17 @@ got=$(typeset -p .foo_nam.three)
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
set +o allexport
# ======
# A namespace's parent scope should be restored after an error occurs.
# https://github.com/ksh93/ksh/issues/479#issuecomment-1140514159
#
# TODO: this test fails when run in a non-forking comsub: the trap
# is not executed -- the zillionth bug with virtual subshells >:-/
exp=$'123 456\n789 456'
got=$(ulimit -t unlimited 2>/dev/null # workaround: force fork
trap 'echo $x ${.test.x}; x=789; echo $x ${.test.x}' EXIT; x=123; namespace test { x=456; set -Q 2>/dev/null; })
[[ $got == "$exp" ]] || err_exit 'Parent scope not restored after special builtin throws error in namespace' \
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
# ======
exit $((Errors<125?Errors:125))