[klibc] [klibc:update-dash] dash: eval: Fix exit status when calling eval/dot with no commands

klibc-bot for Harald van Dijk harald at gigawatt.nl
Sat Mar 28 14:48:47 PDT 2020

Commit-ID:  54653f129781515d3eaff2aa26078ef376bc5a6f
Gitweb:     http://git.kernel.org/?p=libs/klibc/klibc.git;a=commit;h=54653f129781515d3eaff2aa26078ef376bc5a6f
Author:     Harald van Dijk <harald at gigawatt.nl>
AuthorDate: Tue, 7 Jun 2016 16:35:41 +0800
Committer:  Ben Hutchings <ben at decadent.org.uk>
CommitDate: Sat, 28 Mar 2020 21:42:54 +0000

[klibc] dash: eval: Fix exit status when calling eval/dot with no commands

[ dash commit 17a5f24e0a8ec22f74399764db30d97ae310f3c6 ]

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,

     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.

Signed-off-by: Herbert Xu <herbert at gondor.apana.org.au>
Signed-off-by: Ben Hutchings <ben at decadent.org.uk>

 usr/dash/eval.c | 3 ++-
 usr/dash/main.c | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/usr/dash/eval.c b/usr/dash/eval.c
index 3325cb62..0380d1d2 100644
--- a/usr/dash/eval.c
+++ b/usr/dash/eval.c
@@ -172,7 +172,8 @@ 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;
 		if (evalskip)
diff --git a/usr/dash/main.c b/usr/dash/main.c
index bedb6635..497ac160 100644
--- a/usr/dash/main.c
+++ b/usr/dash/main.c
@@ -228,7 +228,8 @@ cmdloop(int top)
 			job_warning = (job_warning == 2) ? 1 : 0;
 			numeof = 0;
 			evaltree(n, 0);
-			status = exitstatus;
+			if (n)
+				status = exitstatus;

More information about the klibc mailing list