From: Mikael Magnusson <mikachu@gmail.com>
To: Peter Stephenson <p.stephenson@samsung.com>
Cc: zsh-workers@zsh.org
Subject: Re: [Bug] Exiting shell from function called by trap handler always produces status 0
Date: Tue, 9 Oct 2018 11:58:05 +0200 [thread overview]
Message-ID: <CAHYJk3Rd1qaOvrA5DusWM4sj-zDT39uQWvZgrFuRyEtrn2=5Vw@mail.gmail.com> (raw)
In-Reply-To: <20181009084918eucas1p27dedda10d51beb773ba9175967912d2c~b5IUtvcVK3037630376eucas1p2R@eucas1p2.samsung.com>
On 10/9/18, Peter Stephenson <p.stephenson@samsung.com> wrote:
> On Mon, 2018-10-08 at 14:02 +0100, Martijn Dekker wrote:
>> When a trap handler exits the shell using the 'exit' command within a
>> function, the shell's exit status is zero even if another exit status
>> was given as an argument to the 'exit' command.
>>
>> $ Src/zsh -c 'fn() { exit 13; }; trap "fn" EXIT'
>> $ echo $?
>> 0
>> (expected output: 13)
>
> diff --git a/Src/builtin.c b/Src/builtin.c
> index c5b319b..b81acdb 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 dce263f..3b5d4c6 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
We might as well make this test
+ $ZTST_testdir/../Src/zsh -fc 'fn() { exit $?+7; }; trap fn EXIT; exit 6'
+13:Explicit exit in exit trap overrides status
--
Mikael Magnusson
next prev parent reply other threads:[~2018-10-09 9:58 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <CGME20181009012624epcas1p44f2ae223f663713a980af4be735e5a3f@epcas1p4.samsung.com>
2018-10-08 13:02 ` Martijn Dekker
2018-10-09 8:49 ` Peter Stephenson
2018-10-09 9:58 ` Mikael Magnusson [this message]
2018-10-09 11:46 ` Martijn Dekker
2018-10-09 13:16 ` Peter Stephenson
2018-10-09 13:39 ` Daniel Shahaf
2018-10-09 13:43 ` Peter Stephenson
[not found] ` <1539092591.3286.12.camel@samsung.com>
2018-10-09 13:50 ` Peter Stephenson
2018-10-09 20:09 ` Peter Stephenson
2018-10-10 8:56 ` Daniel Shahaf
2018-10-10 9:31 ` Peter Stephenson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAHYJk3Rd1qaOvrA5DusWM4sj-zDT39uQWvZgrFuRyEtrn2=5Vw@mail.gmail.com' \
--to=mikachu@gmail.com \
--cc=p.stephenson@samsung.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).