diff --git a/ChangeLog b/ChangeLog
index 23bc06199..cd8a32b85 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2018-10-09 Peter Stephenson
+
+ * 43653: Src/builtin.c, Test/C03traps.ztst: explicit exit from
+ EXIT trap overrides previous status.
+
2018-10-08 a-wing <1@233.email>
* 43623: Completion/Linux/Command/_iptables: Fix Completion
diff --git a/Src/builtin.c b/Src/builtin.c
index c5b319b68..b81acdb53 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -5709,7 +5709,13 @@ int shell_exiting;
mod_export void
zexit(int val, int from_where)
{
- /* Don't do anything recursively: see below */
+ static int exit_val;
+ /*
+ * Don't do anything recursively: see below.
+ * Do, however, update exit status --- there's no nesting,
+ * a later value always overrides an earlier.
+ */
+ exit_val = val;
if (shell_exiting == -1)
return;
@@ -5757,7 +5763,7 @@ zexit(int val, int from_where)
#endif
}
}
- lastval = val;
+ lastval = exit_val;
/*
* Now we are committed to exiting any previous state
* is irrelevant. Ensure trap can run.
@@ -5771,9 +5777,9 @@ zexit(int val, int from_where)
release_pgrp();
}
if (mypid != getpid())
- _exit(val);
+ _exit(exit_val);
else
- exit(val);
+ exit(exit_val);
}
/* . (dot), source */
diff --git a/Test/C03traps.ztst b/Test/C03traps.ztst
index dce263f94..3b5d4c610 100644
--- a/Test/C03traps.ztst
+++ b/Test/C03traps.ztst
@@ -863,6 +863,9 @@ F:Must be tested with a top-level script rather than source or function
>a
>b
+ $ZTST_testdir/../Src/zsh -fc 'fn() { exit 13; }; trap fn EXIT; exit'
+13:Explicit exit in exit trap overrides status
+
%clean
rm -f TRAPEXIT