From patchwork Tue Oct 16 12:09:52 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Antonio Ospite X-Patchwork-Id: 10643525 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C9966109C for ; Tue, 16 Oct 2018 12:54:19 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id BA4B6294F9 for ; Tue, 16 Oct 2018 12:54:19 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A2998289F7; Tue, 16 Oct 2018 12:54:19 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 07E6D289F7 for ; Tue, 16 Oct 2018 12:54:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727002AbeJPUoj (ORCPT ); Tue, 16 Oct 2018 16:44:39 -0400 Received: from mail.ao2.it ([92.243.12.208]:59416 "EHLO ao2.it" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726986AbeJPUoj (ORCPT ); Tue, 16 Oct 2018 16:44:39 -0400 X-Greylist: delayed 2670 seconds by postgrey-1.27 at vger.kernel.org; Tue, 16 Oct 2018 16:44:38 EDT DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=ao2.it; s=20180927; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject:Cc:To:From; bh=CJdwiytiMziOAa6hB6CK2V0ooYWSD1RgNTfjh4it3e4=; b=pWOVLugGHKw5TwF+qbsFFlZm9cqjiL3Q2T9JYPgKRluACRyrapFY3xPpKTaka6ne1tNf2YiKCG7mzB/GYKxIVSlqSR340puZf1OgRGKTl22Uap9X/lbah6icyFLJevjW4V7hLvJoN1dH2P6Xu+Ii9R5u7J6bY54a80Dv4T5a5uEpHlj6P5bcVAz3BpRQKrx3wy8UA58POKGnLIQSJvyeVJyYzP/66fP25AvbS//2vQImdor6EM91BLHZkm9pROonUPnMKa4vLzHqYnoindwzFnVd0ca500Qn7v30CTKwehM/nbM1XrBjQ5vBh3cYlM+aWxdMVGJf/UPCnlGh2jqYEQ==; Received: from localhost ([::1] helo=jcn) by ao2.it with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1gCOAW-0002yM-9m; Tue, 16 Oct 2018 14:09:48 +0200 Received: from ao2 by jcn with local (Exim 4.91) (envelope-from ) id 1gCOAe-0006Yw-FG; Tue, 16 Oct 2018 14:09:56 +0200 From: Antonio Ospite To: dash@vger.kernel.org Cc: Antonio Ospite Subject: [PATCH] eval: make traps work when "set -e" is enabled Date: Tue, 16 Oct 2018 14:09:52 +0200 Message-Id: <20181016120952.25184-1-ao2@ao2.it> X-Mailer: git-send-email 2.19.1 MIME-Version: 1.0 X-Face: z*RaLf`X<@C75u6Ig9}{oW$H;1_\2t5)({*|jhM/Vb;]yA5\I~93>J<_`<4)A{':UrE Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When "set -e" is enabled traps are not always executed, in particular the EXIT trap is not executed when the shell exits on an unhandled error. Consider the following test script: #!/bin/dash set -e trap 'ret=$?; echo "EXIT: $ret"' EXIT trap 'exit 2' HUP INT QUIT PIPE TERM read variable By pressing Ctrl-C one would expect the EXIT trap to be called, as it is the case with other shells (bash, zsh), but dash does not do it. By calling dotrap() before jumping to the exit path when checkexit is not zero, dash behaves like other shells. Signed-off-by: Antonio Ospite --- Hi, this has been reported in Debian[1] and I noticed the issue myself too, so I tried to take a look at it. I am marking the patch as RFC because I don't know the dash codebase very well, and I might not be aware of possible drawbacks of this change. It worked in my limited testing but that's it. I don't know if the behavior of traps is specified when "set -e" is active, but in case it isn't it would stll be good to behave like other shells. Any comment is welcome. Thank you, Antonio [1] https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=779416 src/eval.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/eval.c b/src/eval.c index 6185db4..7d348d0 100644 --- a/src/eval.c +++ b/src/eval.c @@ -307,11 +307,11 @@ setstatus: break; } out: + dotrap(); + if (checkexit & status) goto exexit; - dotrap(); - if (flags & EV_EXIT) { exexit: exraise(EXEXIT);