From a53169826e4d941d454347c0f3bf0f98480dce86 Mon Sep 17 00:00:00 2001 From: Herbert Xu Date: Mon, 17 May 2021 15:19:23 +0800 Subject: eval: Do not cache value of eflag in evaltree MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Patrick Brünn wrote: > > Since we are migrating to Debian bullseye, we discovered a new behavior > with our scripts, which look like this: >>#!/bin/sh >>cleanup() { >> set +e^M >> rmdir "" >>} >>set -eu >>trap 'cleanup' EXIT INT TERM >>echo 'Hello world!' > > With old dash v0.5.10.2 this script would return 0 as we expected it. > But since commit 62cf6955f8abe875752d7163f6f3adbc7e49ebae it returns > the last exit code of our cleanup function. > Reverting that commit gives a merge conflict, but it seems to fix _our_ > problem. As that topic appears too complex to us I want to ask the > experts here: > > Is this change in behavior intended, by dash? > > Our workaround at the moment would be: >>trap 'cleanup || true' EXIT INT TERM Thanks for the report. This is actually a fairly old bug with set -e that's just been exposed by the exit status change. What's really happening is that cleanup itself is triggering a set -e exit incorrectly because evaltree cached the value of eflag prior to the function call. This patch should fix the problem. Reported-by: Patrick Brünn Signed-off-by: Herbert Xu Tested-by: Patrick Brünn Signed-off-by: Herbert Xu --- src/eval.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/eval.c b/src/eval.c index 1b5d61d..d4190f9 100644 --- a/src/eval.c +++ b/src/eval.c @@ -245,18 +245,10 @@ evaltree(union node *n, int flags) popredir(0); goto setstatus; case NCMD: -#ifdef notyet - if (eflag && !(flags & EV_TESTED)) - checkexit = ~0; - status = evalcommand(n, flags, (struct backcmd *)NULL); - goto setstatus; -#else evalfn = evalcommand; checkexit: - if (eflag && !(flags & EV_TESTED)) - checkexit = ~0; + checkexit = ~flags & EV_TESTED; goto calleval; -#endif case NFOR: evalfn = evalfor; goto calleval; @@ -316,7 +308,7 @@ setstatus: out: dotrap(); - if (checkexit & status) + if (eflag && checkexit && status) goto exexit; if (flags & EV_EXIT) { -- cgit 1.4.1