zsh-workers
 help / color / mirror / code / Atom feed
* Why do we need to quote # in ${(#):-2\#1011010} ?
@ 2021-09-12  8:11 Stephane Chazelas
  2021-09-12 17:43 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Stephane Chazelas @ 2021-09-12  8:11 UTC (permalink / raw)
  To: Zsh hackers list

$ echo ${:-2#1011010}
2#1011010

That # is fine when not quoted there, so why:

$ echo ${(#):-2#1011010}

$ echo ${(#):-2\#1011010}
Z

?

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

-- 
Stephane


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Why do we need to quote # in ${(#):-2\#1011010} ?
  2021-09-12  8:11 Why do we need to quote # in ${(#):-2\#1011010} ? Stephane Chazelas
@ 2021-09-12 17:43 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2021-09-12 17:43 UTC (permalink / raw)
  To: Zsh hackers list

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"


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-09-12 17:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-12  8:11 Why do we need to quote # in ${(#):-2\#1011010} ? Stephane Chazelas
2021-09-12 17:43 ` Bart Schaefer

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