zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Why do we need to quote # in ${(#):-2\#1011010} ?
Date: Sun, 12 Sep 2021 10:43:45 -0700	[thread overview]
Message-ID: <CAH+w=7aBnhS=mKQfobfRpQiuN376BwZ2dtXoydrmXfCn1gK7ow@mail.gmail.com> (raw)
In-Reply-To: <20210912081151.k5cqsw6tdmkj7mi4@chazelas.org>

On Sun, Sep 12, 2021 at 1:12 AM Stephane Chazelas <stephane@chazelas.org> wrote:
>
> $ echo ${:-2#1011010}
> 2#1011010
>
> That # is fine when not quoted there, so why:
> [...]
> It seems to discard everything after the expansion within the word:
>
> $ echo before${(#):-2#1011010}"hey, where did that go?" other args
> before other args

The short answer is that ${(#):-2#1011010} should give a parse error
on the number-to-character conversion, but doesn't.  In the first
case, without (#), there's no parse so there's no error, which is why
it doesn't need the backslash.  I suppose paramsubst() should be
calling zerr() somewhere near here:

   3553         noerrs = one;
   3554         if (!quoteerr) {
   3555             /* Retain user interrupt error status */
   3556             errflag = oef | (errflag & ERRFLAG_INT);
   3557         }
   3558         if (haserr || errflag)
   3559             return NULL;

The other option would be to unmetafy somewhere before calling
substevalchar() but since multsub() also sometimes unmetafies, it's
not clear when to do so.  Or perhaps something upstream is
unnecessarily over-metafying when the # is not escaped?

More details (just notes as I step through with gdb):


FAILING CASE

We arrive in paramsubst() with
"\205\217\210\204\212:\233\062\204\061\060\061\061\060\061\060\220\236hey,
where did that go?\236"

val becomes "2\204\061\060\061\061\060\061\060" at line 2948

multsub() does nothing, so substevalchar() returns NUL at 3550 and haserr = 1

so paramsubst() returns NULL as does stringsubst(), but without an errflag

and prefork() bails out at line 146, which truncates the word.


WITHOUT (#)

"\205\217:\233\062\204\061\060\061\061\060\061\060\220\236hey, where
did that go?\236"

Everything is the same as the failing case except that substevalchar()
is never called, so there's no error, and the result is simply
unmetafied and returned.


WITH BACKSLASH / WORKING

"\205\217\210\204\212:\233\062\237#1011010\220\236hey, where did that go?\236"

val becomes "2\237#1011010"

multsub() unmetafies it to "2#1011010" and substevalchar() returns "Z"


      reply	other threads:[~2021-09-12 17:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-12  8:11 Stephane Chazelas
2021-09-12 17:43 ` Bart Schaefer [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:
  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='CAH+w=7aBnhS=mKQfobfRpQiuN376BwZ2dtXoydrmXfCn1gK7ow@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

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