From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: zsh-workers-return-43657-ml=inbox.vuxu.org@zsh.org X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id 810cfe67 for ; Tue, 9 Oct 2018 09:58:24 +0000 (UTC) Received: (qmail 23571 invoked by alias); 9 Oct 2018 09:58:12 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: X-Seq: 43657 Received: (qmail 4080 invoked by uid 1010); 9 Oct 2018 09:58:12 -0000 X-Qmail-Scanner-Diagnostics: from mail-qt1-f194.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(209.85.160.194):SA:0(-1.9/5.0):. Processed in 2.118338 secs); 09 Oct 2018 09:58:12 -0000 X-Envelope-From: mikachu@gmail.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=anUhyhoBbUzOXi20XhoDRmpxXxmpOYTlGW17Horcxdo=; b=DEXwGhADIRdISeHj2aES+FHhGEizLcCJqQqqJqRhODche96SVYez4TDVKLMk3bwJNU slu1cRZCTv9q44R8/nrDl1+8bMlQ+caK6ZUjDfdfoOWt8o365ah1RkzlN5TaDsKec3YS ewYwEa1vCPr5tC8DVC1meYBeFH9Tdmr5QRB8CnYdzss1vJ5FHusENebeI2NKQS3fokyM 29zeIvIbtE5reqYN1W2UtEeyGJ+0nokHU4BfWK87bEB6wUjUqphOcq0ul1WtjYPzDnRi 7UPmbiW36HRxdTvFxGPi9LYq//nFxNMZMC2ptEXFyzu4YAGbrysjjvaTCHhHuHkZGlk2 RkDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=anUhyhoBbUzOXi20XhoDRmpxXxmpOYTlGW17Horcxdo=; b=Z+DBdFoQI04F1C2lPpDh7vw9I0eQ/lWEllHeFElxCe9p4X4dODZnFBGyx85+auCo9S TVUjMwTDvfuxH6bucQtXbyaGij+FU4tFzkdfAOg2iiv4nSKXj3IZO/39jvWTzQ5vtNKc eRFjOXIk3ud6lY74pP9smeiN33iYbY9SLO99ayyQbf2Sjsz8hV1Zb3TDcPn4PKNzj/Ci xn8QexKgKtZA8ELW8COtCXtTMfE4tWJgqzdjyhAnoLYnZHviMLUUuKI31rdziu0UN6Bc v+8RlszecrFq1mvulgJCYy1UvzW7qT0o8fLPkdeMgDCSLTV3q4fN2e138OB84XXfzTGF y0IA== X-Gm-Message-State: ABuFfoh959GReJNGxJ0ySHk03mMXluH8L3Hq4SuBTVreU0dhHUP4Lm3Q GSFPeHU8c7HPMjLdUhbaAZnXn6GC0DCKavZlZtTPuQ== X-Google-Smtp-Source: ACcGV63UAFh31JvuZRK/jtpIjVVzo0OZD4a09WBmZu9Jd/4krf7X9jFWdGmILkuKLXQOh9lQlIeY1qYEgyRV2QmqWaU= X-Received: by 2002:a0c:8757:: with SMTP id 23mr8224980qvi.223.1539079086274; Tue, 09 Oct 2018 02:58:06 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <20181009084918eucas1p27dedda10d51beb773ba9175967912d2c~b5IUtvcVK3037630376eucas1p2R@eucas1p2.samsung.com> References: <20181009084918eucas1p27dedda10d51beb773ba9175967912d2c~b5IUtvcVK3037630376eucas1p2R@eucas1p2.samsung.com> From: Mikael Magnusson Date: Tue, 9 Oct 2018 11:58:05 +0200 Message-ID: Subject: Re: [Bug] Exiting shell from function called by trap handler always produces status 0 To: Peter Stephenson Cc: zsh-workers@zsh.org Content-Type: text/plain; charset="UTF-8" On 10/9/18, Peter Stephenson 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