summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilippe Altherr <philippe.altherr@gmail.com>2026-05-09 13:36:54 -0700
committerBart Schaefer <schaefer@zsh.org>2026-05-09 13:36:54 -0700
commit64f8448103463999c5b75d7c01f39f31e2dd31fd (patch)
treef0a5460d8964da3d2378c5ee7298acad05c4ac2b
parent54484: tests: fix -nt test on low-resolution file systems (diff)
downloadzsh-64f8448103463999c5b75d7c01f39f31e2dd31fd.tar
zsh-64f8448103463999c5b75d7c01f39f31e2dd31fd.tar.gz
zsh-64f8448103463999c5b75d7c01f39f31e2dd31fd.tar.bz2
zsh-64f8448103463999c5b75d7c01f39f31e2dd31fd.tar.lz
zsh-64f8448103463999c5b75d7c01f39f31e2dd31fd.tar.xz
zsh-64f8448103463999c5b75d7c01f39f31e2dd31fd.tar.zst
zsh-64f8448103463999c5b75d7c01f39f31e2dd31fd.zip
54374: Run ZERR trap on zsh errors
-rw-r--r--ChangeLog5
-rw-r--r--Src/exec.c3
-rw-r--r--Test/C03traps.ztst75
3 files changed, 83 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 09c83d036..e4c049f8f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2026-05-09 Bart Schaefer <schaefer@zsh.org>
+
+ * Philippe Altherr: 54374: Src/exec.c Test/C03traps.ztst: Run
+ ZERR trap on zsh errors as well as external command errors
+
2026-05-06 dana <dana@dana.is>
* 54484: Test/C02cond.ztst: fix -nt test on low-resolution file
diff --git a/Src/exec.c b/Src/exec.c
index 47d70b5a9..60075f254 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -1598,7 +1598,10 @@ sublist_done:
if (!this_noerrexit && !donetrap && !this_donetrap) {
if (sigtrapped[SIGZERR] && lastval &&
!(noerrexit & NOERREXIT_EXIT)) {
+ int eflag = errflag;
+ errflag = 0;
dotrap(SIGZERR);
+ errflag = eflag;
donetrap = 1;
}
if (lastval) {
diff --git a/Test/C03traps.ztst b/Test/C03traps.ztst
index 87b7fd1f7..9924e4f96 100644
--- a/Test/C03traps.ztst
+++ b/Test/C03traps.ztst
@@ -361,6 +361,81 @@
>trap
>Working 0
+ ok() { true } # All good
+ ko() { false } # Just a non-zero status
+ KO() { { : $KO } 2>&1 } # A Zsh error
+ fn() {
+ echo "$1"
+ set -u;
+ case $1 in
+ *-TRAP-ok) TRAPZERR() { echo "ok 1"; ok; echo "ok 2" };;
+ *-trap-ok) trap 'echo "ok 1"; ok; echo "ok 2"' ZERR;;
+ *-TRAP-KO) TRAPZERR() { echo "ok 1"; KO; echo "KO 2" };;
+ *-trap-KO) trap 'echo "ok 1"; KO; echo "KO 2"' ZERR;;
+ * ) echo "Unrecognized id: $1";
+ esac
+ case $1 in
+ ko-*-*) ko; echo "ok 3";;
+ KO-*-*) KO; echo "KO 3";;
+ * ) echo "Unrecognized id: $1";
+ esac
+ }
+ (fn ko-TRAP-ok; echo "ok 4"); echo "\$?=$?"
+ (fn ko-trap-ok; echo "ok 4"); echo "\$?=$?"
+ (fn ko-TRAP-KO; echo "ok 4"); echo "\$?=$?"
+ (fn ko-trap-KO; echo "ok 4"); echo "\$?=$?"
+ (fn KO-TRAP-ok; echo "KO 4"); echo "\$?=$?"
+ (fn KO-trap-ok; echo "KO 4"); echo "\$?=$?"
+ (fn KO-TRAP-KO; echo "KO 4"); echo "\$?=$?"
+ (fn KO-trap-KO; echo "KO 4"); echo "\$?=$?"
+0:DDD
+>ko-TRAP-ok
+>ok 1
+>ok 2
+>ok 3
+>ok 4
+>$?=0
+>ko-trap-ok
+>ok 1
+>ok 2
+>ok 3
+>ok 4
+>$?=0
+>ko-TRAP-KO
+>ok 1
+>KO: KO: parameter not set
+F:Shouldn't the error in the trap prevent any further execution?
+>ok 3
+>ok 4
+>$?=0
+>ko-trap-KO
+>ok 1
+>KO: KO: parameter not set
+F:Shouldn't the error in the trap prevent any further execution?
+>ok 3
+>ok 4
+>$?=0
+>KO-TRAP-ok
+>KO: KO: parameter not set
+>ok 1
+>ok 2
+>$?=1
+>KO-trap-ok
+>KO: KO: parameter not set
+>ok 1
+>ok 2
+>$?=1
+>KO-TRAP-KO
+>KO: KO: parameter not set
+>ok 1
+>KO: KO: parameter not set
+>$?=1
+>KO-trap-KO
+>KO: KO: parameter not set
+>ok 1
+>KO: KO: parameter not set
+>$?=1
+
{ trap 'echo This subshell is exiting' EXIT; } | cat
0: EXIT trap set in current shell at left of pipeline
>This subshell is exiting