mirror of
git://git.code.sf.net/p/cdesktopenv/code
synced 2025-02-15 04:32:24 +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:
parent
8f14514661
commit
f73b8617dd
4 changed files with 28 additions and 2 deletions
5
NEWS
5
NEWS
|
@ -3,6 +3,11 @@ For full details, see the git log at: https://github.com/ksh93/ksh/tree/1.0
|
||||||
|
|
||||||
Any uppercase BUG_* names are modernish shell bug IDs.
|
Any uppercase BUG_* names are modernish shell bug IDs.
|
||||||
|
|
||||||
|
2022-05-29:
|
||||||
|
|
||||||
|
- Fixed a bug causing an inconsistent state after a special built-in command
|
||||||
|
threw an error within a namespace.
|
||||||
|
|
||||||
2022-05-27:
|
2022-05-27:
|
||||||
|
|
||||||
- Fixed a bug introduced on 2022-03-05 where 'set --default', while turning
|
- Fixed a bug introduced on 2022-03-05 where 'set --default', while turning
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
|
|
||||||
#define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */
|
#define SH_RELEASE_FORK "93u+m" /* only change if you develop a new ksh93 fork */
|
||||||
#define SH_RELEASE_SVER "1.0.0-beta.2" /* semantic version number: https://semver.org */
|
#define SH_RELEASE_SVER "1.0.0-beta.2" /* semantic version number: https://semver.org */
|
||||||
#define SH_RELEASE_DATE "2022-05-27" /* must be in this format for $((.sh.version)) */
|
#define SH_RELEASE_DATE "2022-05-29" /* must be in this format for $((.sh.version)) */
|
||||||
#define SH_RELEASE_CPYR "(c) 2020-2022 Contributors to ksh " SH_RELEASE_FORK
|
#define SH_RELEASE_CPYR "(c) 2020-2022 Contributors to ksh " SH_RELEASE_FORK
|
||||||
|
|
||||||
/* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */
|
/* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */
|
||||||
|
|
|
@ -2487,6 +2487,8 @@ int sh_exec(register const Shnode_t *t, int flags)
|
||||||
Namval_t *oldnspace = sh.namespace;
|
Namval_t *oldnspace = sh.namespace;
|
||||||
int offset = stktell(stkp);
|
int offset = stktell(stkp);
|
||||||
int flags=NV_NOASSIGN|NV_NOARRAY|NV_VARNAME;
|
int flags=NV_NOASSIGN|NV_NOARRAY|NV_VARNAME;
|
||||||
|
struct checkpt *chkp = (struct checkpt*)stakalloc(sizeof(struct checkpt));
|
||||||
|
int jmpval;
|
||||||
if(cp)
|
if(cp)
|
||||||
{
|
{
|
||||||
errormsg(SH_DICT,ERROR_exit(1),e_ident,fname);
|
errormsg(SH_DICT,ERROR_exit(1),e_ident,fname);
|
||||||
|
@ -2511,8 +2513,15 @@ int sh_exec(register const Shnode_t *t, int flags)
|
||||||
dtview(root,sh.var_base);
|
dtview(root,sh.var_base);
|
||||||
}
|
}
|
||||||
oldnspace = enter_namespace(np);
|
oldnspace = enter_namespace(np);
|
||||||
|
/* make sure to restore oldnspace if a special builtin throws an error */
|
||||||
|
sh_pushcontext(chkp,SH_JMPCMD);
|
||||||
|
jmpval = sigsetjmp(chkp->buff,1);
|
||||||
|
if(!jmpval)
|
||||||
sh_exec(t->for_.fortre,flags|sh_state(SH_ERREXIT));
|
sh_exec(t->for_.fortre,flags|sh_state(SH_ERREXIT));
|
||||||
|
sh_popcontext(chkp);
|
||||||
enter_namespace(oldnspace);
|
enter_namespace(oldnspace);
|
||||||
|
if(jmpval) /* error occurred */
|
||||||
|
sh_exit(sh.exitval);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#endif /* SHOPT_NAMESPACE */
|
#endif /* SHOPT_NAMESPACE */
|
||||||
|
|
|
@ -130,5 +130,17 @@ got=$(typeset -p .foo_nam.three)
|
||||||
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
|
"(expected $(printf %q "$exp"), got $(printf %q "$got"))"
|
||||||
set +o allexport
|
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))
|
exit $((Errors<125?Errors:125))
|
||||||
|
|
Loading…
Reference in a new issue