dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Harald van Dijk <harald@gigawatt.nl>
To: Gioele Barabucci <gioele@svario.it>
Cc: dash@vger.kernel.org
Subject: Re: Sourcing an empty files does not reset exit status
Date: Sat, 5 Dec 2015 16:40:59 +0100	[thread overview]
Message-ID: <5663058B.1030809@gigawatt.nl> (raw)
In-Reply-To: <n2e2qe$voq$1@ger.gmane.org>

[-- Attachment #1: Type: text/plain, Size: 1991 bytes --]

On 17/11/2015 03:18, Gioele Barabucci wrote:
> Hello,
>
> a bug has been filed in the Debian BTS about dash not resetting the exit
> status after sourcing an empty file with the dot command. [1]
>
> The following test echoes "OK" with bash and "fail" with dash
>
>      #!/bin/sh
>
>      echo > ./empty
>      false
>
>      . ./empty && echo "OK" || echo "fail"
 >
> A similar bug in dash has been discussed and addressed in 2011 [2], but
> it looks like the solution has been only partial.
>
> The version of dash I tested is the current git master branch, commit
> 2e58422.
>
> [1] https://bugs.debian.org/777262
> [2] http://article.gmane.org/gmane.comp.shells.dash/531

The bug described there was about empty files. While the fix has been 
applied and does make dash handle empty files properly, your test 
doesn't use an empty file, it uses a file containing a single blank 
line. Unfortunately, the single blank line gets parsed by dash as a null 
command, null commands don't (and shouldn't) reset the exit status, and 
the fix you link to doesn't handle this because it sees a command has 
been executed and saves the exit status after executing that command as 
the exit status to be used by ".".

I think the easiest way to fix this is to prevent null commands from 
affecting status in cmdloop, as attached.

An alternative could be to change the outer if condition to exclude
n == NULL, but I didn't do that because the change of job_warning and 
clearing of numeof make sense to me even for null commands. Besides, 
when debug tracing is enabled, null commands have a visible effect that 
should remain.

Note that this fixes the problem with . but the same problem can be 
present in other locations. For example,

     false
     eval "
     " && echo OK || echo Fail

used to print Fail, and needed the same modification in the evalstring 
function to make that print OK (included in the attached patch). There 
may be other similar bugs lurking.

Cheers,
Harald van Dijk

[-- Attachment #2: dash-dot-status.patch --]
[-- Type: text/plain, Size: 676 bytes --]

diff --git a/src/eval.c b/src/eval.c
index 071fb1b..46689ef 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -172,7 +172,7 @@ evalstring(char *s, int flags)
 	status = 0;
 	while ((n = parsecmd(0)) != NEOF) {
 		evaltree(n, flags & ~(parser_eof() ? 0 : EV_EXIT));
-		status = exitstatus;
+		if (n) status = exitstatus;
 		popstackmark(&smark);
 		if (evalskip)
 			break;
diff --git a/src/main.c b/src/main.c
index bedb663..1b301fa 100644
--- a/src/main.c
+++ b/src/main.c
@@ -228,7 +228,7 @@ cmdloop(int top)
 			job_warning = (job_warning == 2) ? 1 : 0;
 			numeof = 0;
 			evaltree(n, 0);
-			status = exitstatus;
+			if (n) status = exitstatus;
 		}
 		popstackmark(&smark);
 

  reply	other threads:[~2015-12-05 15:41 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-17  2:18 Gioele Barabucci
2015-12-05 15:40 ` Harald van Dijk [this message]
2016-06-06 10:13   ` [PATCH] eval: Return status in eval functions Herbert Xu
2016-06-06 21:14     ` Harald van Dijk
2016-06-07  8:47       ` [PATCH v2] " Herbert Xu

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=5663058B.1030809@gigawatt.nl \
    --to=harald@gigawatt.nl \
    --cc=dash@vger.kernel.org \
    --cc=gioele@svario.it \
    --subject='Re: Sourcing an empty files does not reset exit status' \
    /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

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).