From 6dbde5ec7c674876670dc13abf154f7c58c09a3b Mon Sep 17 00:00:00 2001 From: Martijn Dekker Date: Thu, 2 Jun 2022 03:19:16 +0100 Subject: [PATCH] Correctly exit from namespace on error (re: f73b8617) The referenced commit introduced at least one bug: EXIT traps were not triggered if a special builtin threw an error in a namespace that is within a virtual subshell. src/cmd/ksh93/sh/xec.c: sh_exec(): TNSPACE: - When an error occurs, siglongjmp to the previous saved environment; it is not correct to sh_exit directly. src/cmd/ksh93/tests/namespace.sh: - Remove the forkign workaround and the TODO where I incorrectly blamed this problem on the virtual subshell mechanism. --- src/cmd/ksh93/sh/xec.c | 2 +- src/cmd/ksh93/tests/namespace.sh | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/cmd/ksh93/sh/xec.c b/src/cmd/ksh93/sh/xec.c index 805a4aee0..474961b93 100644 --- a/src/cmd/ksh93/sh/xec.c +++ b/src/cmd/ksh93/sh/xec.c @@ -2523,7 +2523,7 @@ int sh_exec(register const Shnode_t *t, int flags) sh_popcontext(chkp); enter_namespace(oldnspace); if(jmpval) /* error occurred */ - sh_exit(sh.exitval); + siglongjmp(*sh.jmplist,jmpval); break; } #endif /* SHOPT_NAMESPACE */ diff --git a/src/cmd/ksh93/tests/namespace.sh b/src/cmd/ksh93/tests/namespace.sh index 5c5978345..74d30d35d 100755 --- a/src/cmd/ksh93/tests/namespace.sh +++ b/src/cmd/ksh93/tests/namespace.sh @@ -133,11 +133,8 @@ 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 +got=$( trap 'echo $x ${.test.x}; x=789; echo $x ${.test.x}' EXIT x=123 namespace test