diff --git a/NEWS b/NEWS index 16b751465..5cec75821 100644 --- a/NEWS +++ b/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. +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: - Fixed a bug introduced on 2022-03-05 where 'set --default', while turning diff --git a/src/cmd/ksh93/include/version.h b/src/cmd/ksh93/include/version.h index e2bd413c8..bd0267e09 100644 --- a/src/cmd/ksh93/include/version.h +++ b/src/cmd/ksh93/include/version.h @@ -21,7 +21,7 @@ #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_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 /* Scripts sometimes field-split ${.sh.version}, so don't change amount of whitespace. */ diff --git a/src/cmd/ksh93/sh/xec.c b/src/cmd/ksh93/sh/xec.c index f0ccc7ea0..6f18874af 100644 --- a/src/cmd/ksh93/sh/xec.c +++ b/src/cmd/ksh93/sh/xec.c @@ -2487,6 +2487,8 @@ int sh_exec(register const Shnode_t *t, int flags) Namval_t *oldnspace = sh.namespace; int offset = stktell(stkp); int flags=NV_NOASSIGN|NV_NOARRAY|NV_VARNAME; + struct checkpt *chkp = (struct checkpt*)stakalloc(sizeof(struct checkpt)); + int jmpval; if(cp) { 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); } oldnspace = enter_namespace(np); - sh_exec(t->for_.fortre,flags|sh_state(SH_ERREXIT)); + /* 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_popcontext(chkp); enter_namespace(oldnspace); + if(jmpval) /* error occurred */ + sh_exit(sh.exitval); break; } #endif /* SHOPT_NAMESPACE */ diff --git a/src/cmd/ksh93/tests/namespace.sh b/src/cmd/ksh93/tests/namespace.sh index 1d596d53a..e9adc88ab 100755 --- a/src/cmd/ksh93/tests/namespace.sh +++ b/src/cmd/ksh93/tests/namespace.sh @@ -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))