about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPeter Stephenson <p.stephenson@samsung.com>2018-10-09 10:05:05 +0100
committerPeter Stephenson <p.stephenson@samsung.com>2018-10-09 10:05:05 +0100
commitd768a7492a850e8d1ccb00f2cc8eef0bd238b7ab (patch)
tree27a59d6605abdb9d32a2dd31c26fd147b181085d
parent00baf08602705f1edc13266c24ecf2df5241143b (diff)
downloadzsh-d768a7492a850e8d1ccb00f2cc8eef0bd238b7ab.tar.gz
zsh-d768a7492a850e8d1ccb00f2cc8eef0bd238b7ab.tar.xz
zsh-d768a7492a850e8d1ccb00f2cc8eef0bd238b7ab.zip
43653: explicit exit from EXIT trap overrides exit status
-rw-r--r--ChangeLog5
-rw-r--r--Src/builtin.c14
-rw-r--r--Test/C03traps.ztst3
3 files changed, 18 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 23bc06199..cd8a32b85 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2018-10-09  Peter Stephenson  <p.stephenson@samsung.com>
+
+	* 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