diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:18:38 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-28 00:18:38 -0600 |
| commit | 9cdf5f548dc8e1d5c6315b304819bf0fd60f802f (patch) | |
| tree | d7a67ca6c3aec6f387fc69b6f1931955e6e116e1 | |
| parent | 0fc285274b7dacbba311bdbd4b4697b7ebcac09c (diff) | |
| download | zash-9cdf5f548dc8e1d5c6315b304819bf0fd60f802f.tar.gz | |
fix(exec): run the ERR trap when the last command is exec'd
`trap "echo ERR" ERR; /bin/false` printed nothing: the shell replaced
itself with the failing program and was gone before the trap could run.
The same shortcut made `trap ... ; (/bin/false)` silently skip it.
trap_after_last() is true when the EXIT or the ERR trap is set, and both
the exec shortcut in run_external() and the in-process subshell shortcut
in eval_subshell() now use it instead of trap_has_exit().
tests/zash/errtrap-exec.sh covers the external command, a builtin, a
subshell and the two traps together; the first and third lines fail
without the change. ksh93 prints ERR for all four.
| -rw-r--r-- | src/exec.c | 4 | ||||
| -rw-r--r-- | src/exec.h | 1 | ||||
| -rw-r--r-- | src/trap.c | 13 | ||||
| -rw-r--r-- | tests/zash/errtrap-exec.exp | 10 | ||||
| -rw-r--r-- | tests/zash/errtrap-exec.sh | 8 |
5 files changed, 34 insertions, 2 deletions
@@ -957,7 +957,7 @@ static int run_external(simple_cmd *sc, unsigned flags, int in, int out, plan_close(&plan); return 2; } - if ((flags & EV_EXIT) && !async && !trap_has_exit()) { + if ((flags & EV_EXIT) && !async && !trap_after_last()) { /* Nothing comes after this command: become it. */ plan_apply(&plan); exec_path(sc->cmd.path, sc->argv, envp); @@ -1908,7 +1908,7 @@ static int eval_case(node *n, unsigned flags) { * the shell was about to exit anyway. */ static int eval_subshell(node *n, unsigned flags) { - if (!(flags & EV_EXIT) || trap_has_exit()) { + if (!(flags & EV_EXIT) || trap_after_last()) { if (!job_launching && !opt.errexit && subshell_in_process(n)) { simple_cmd sc; varscope vs; @@ -215,6 +215,7 @@ void trap_poll(void); void trap_reset_child(void); void trap_reset_exec(void); bool trap_has_exit(void); +bool trap_after_last(void); void trap_run_exit(void); const sigset_t *trap_spawn_defaults(void); int builtin_trap(int argc, char **argv); @@ -398,6 +398,19 @@ bool trap_has_exit(void) { } /** + * @brief Must something run after the last command? + * + * The EXIT trap and the ERR trap both run after the command that ends the + * shell, so when either is set the last command may not replace the + * shell: the process would be gone before the trap could run. + * + * @return true if the EXIT or the ERR trap is set. + */ +bool trap_after_last(void) { + return trap_has_exit() || trap_err_set; +} + +/** * @brief Run the EXIT trap, once. */ void trap_run_exit(void) { diff --git a/tests/zash/errtrap-exec.exp b/tests/zash/errtrap-exec.exp new file mode 100644 index 0000000..216c976 --- /dev/null +++ b/tests/zash/errtrap-exec.exp @@ -0,0 +1,10 @@ +ERR +[1] +ERR +[1] +ERR +[1] +ERR +BYE +[1] +[status 0] diff --git a/tests/zash/errtrap-exec.sh b/tests/zash/errtrap-exec.sh new file mode 100644 index 0000000..3acf6e1 --- /dev/null +++ b/tests/zash/errtrap-exec.sh @@ -0,0 +1,8 @@ +# The ERR trap runs for the last command too. A failing external command +# as the last thing a shell runs is exec'd, and the shell is gone before +# the trap could run, unless the trap holds that shortcut back. So does a +# subshell that would otherwise run in the parent. +"$TSH" -c 'trap "echo ERR" ERR; /bin/false'; echo "[$?]" +"$TSH" -c 'trap "echo ERR" ERR; false'; echo "[$?]" +"$TSH" -c 'trap "echo ERR" ERR; (/bin/false)'; echo "[$?]" +"$TSH" -c 'trap "echo ERR" ERR; trap "echo BYE" EXIT; /bin/false'; echo "[$?]" |
