dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Harald van Dijk <harald@gigawatt.nl>
To: DASH shell mailing list <dash@vger.kernel.org>,
	Martijn Dekker <martijn@inlv.org>
Subject: Re: Another alias substitution bug, now involving case statements
Date: Sat, 25 Jan 2020 17:55:59 +0000	[thread overview]
Message-ID: <0f37bf49-bda8-bc55-f235-b5f6987590b6@gigawatt.nl> (raw)
In-Reply-To: <f35c1096-18b2-f438-f6d7-19c9d5e335ab@gigawatt.nl>

On 25/01/2020 17:29, Harald van Dijk wrote:
> Hi,
> Checking alias substitution handling in more detail after Martijn 
> Dekker's report, I found another case where I believe dash's behaviour 
> is incorrect. First, please consider this test:
>    alias a="case x in " b=x
>    a b) echo hi ;; esac
> bash, ksh and pdksh never check whether a case pattern is a valid alias 
> name, so they print nothing.
> bosh, dash, mksh, yash and zsh do allow the space at the end of a's 
> definition to cause b to be considered as an alias name.
> Based on the literal text of the current standard, I believe 
> bash/ksh/pdksh's behaviour is correct. Based on accepted new wording for 
> the standard, new wording that was drafted without considering this 
> special case, I believe the bosh/dash/mksh/yash/zsh behaviour is 
> correct. I would not at this time consider this a bug in any of the shells.
> Now, consider this modified version:
>    alias a="case x in " b=x
>    a
>    b) echo hi ;; esac
> Here, the next token after "a" is a newline token, not b. Here, b must 
> definitely not be considered as an alias name. bosh, dash, mksh and zsh 
> do perform alias substitution anyway, yash does not.
> The problem here is in the "eat newlines" behaviour of readtoken(). 
> There are two reasons why CHKALIAS might be set. It might be set because 
> the parser is in a state where the next token could be the start of a 
> simple command, or it might be set because the parser processed a blank 
> at the end of a prior alias definition. In the first case, after eating 
> a newline, the parser is still in a state where the next token could be 
> the start of a simple command, so CHKALIAS should not be dropped. In the 
> second case, the blank should only affect a single token, and upon 
> eating a newline CHKALIAS should be dropped. readtoken() has no way of 
> distinguishing between these two cases with just a single CHKALIAS flag, 
> so this will require a bit more complicated work to fix.

Not very complicated after all. One flag in kwd and one flag in checkkwd 
will do the job. kwd tracks whether CHKALIAS was set prior to the call 
to readtoken(), i.e. whether this is potentially the start of a simple 
command. checkkwd tracks whether CHKALIAS was/got set at the last 
xxreadtoken() call.

--- a/src/parser.c
+++ b/src/parser.c
@@ -713,6 +713,7 @@ top:
         if (kwd & CHKNL) {
                 while (t == TNL) {
+                       checkkwd = 0;
                         t = xxreadtoken();
@@ -734,7 +735,7 @@ top:

-       if (checkkwd & CHKALIAS) {
+       if ((checkkwd | kwd) & CHKALIAS) {
                 struct alias *ap;
                 if ((ap = lookupalias(wordtext, 1)) != NULL) {
                         if (*ap->val) {

I have checked that this handles both my cases here and the test cases 
in Martijn Dekker's thread.

> Cheers,
> Harald van Dijk

      reply	other threads:[~2020-01-25 17:57 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-25 17:29 Harald van Dijk
2020-01-25 17:55 ` Harald van Dijk [this message]

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:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0f37bf49-bda8-bc55-f235-b5f6987590b6@gigawatt.nl \
    --to=harald@gigawatt.nl \
    --cc=dash@vger.kernel.org \
    --cc=martijn@inlv.org \
    --subject='Re: Another alias substitution bug, now involving case statements' \


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