From patchwork Sat Dec 5 15:40:59 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald van Dijk X-Patchwork-Id: 7775571 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: X-Original-To: patchwork-dash@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id DE70A9F1C2 for ; Sat, 5 Dec 2015 15:41:16 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 2AF3E204D9 for ; Sat, 5 Dec 2015 15:41:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 0891E204D5 for ; Sat, 5 Dec 2015 15:41:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753376AbbLEPlJ (ORCPT ); Sat, 5 Dec 2015 10:41:09 -0500 Received: from mailfilter1-k0683s008-2.csv-networks.nl ([92.48.231.158]:53640 "EHLO mailfilter1-k0683s008.csv-networks.nl" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1753352AbbLEPlI (ORCPT ); Sat, 5 Dec 2015 10:41:08 -0500 Received: from [84.244.151.217] (helo=hosting12.csv-networks.nl) by mailfilter1-k0683s008.csv-networks.nl with esmtps (TLSv1:AES256-SHA:256) (Exim 4.72) (envelope-from ) id 1a5EyG-0005yt-BP; Sat, 05 Dec 2015 15:42:00 +0000 Received: from home.gigawatt.nl ([83.163.3.213] helo=[192.168.178.26]) by hosting12.csv-networks.nl with esmtpsa (TLSv1:DHE-RSA-AES128-SHA:128) (Exim 4.85) (envelope-from ) id 1a5ExG-00016D-Jr; Sat, 05 Dec 2015 16:40:58 +0100 Subject: Re: Sourcing an empty files does not reset exit status To: Gioele Barabucci References: From: Harald van Dijk Cc: dash@vger.kernel.org Message-ID: <5663058B.1030809@gigawatt.nl> Date: Sat, 5 Dec 2015 16:40:59 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 MIME-Version: 1.0 In-Reply-To: Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, T_TVD_MIME_EPI, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 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);