zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: Example / partial fix for printf with math expressions
Date: Sat, 24 Feb 2024 13:51:18 -0800	[thread overview]
Message-ID: <CAH+w=7aYkchMeJX9+jXY1Ev2wDuDAqqAq=DKRG110xOs+UFWxw@mail.gmail.com> (raw)
In-Reply-To: <20240224144041.7huny3lmihsrokdm@chazelas.org>

Thanks for the builtin.c patch.  Just a couple of notes:

On Sat, Feb 24, 2024 at 6:40 AM Stephane Chazelas
<stephane@chazelas.org> wrote (with correction from a later message):
>
> The math parser seems to work OK with NULs at least in:
>
> $ typeset -A a
> $ let $'a[\0]++'

That's not exactly the math parser; mathparse() get as far as noticing
it's a parameter reference and then goes through
params.c:getnumvalue() which actually does the subscript parsing.
You're "cheating" with this construction (and of course "let" is doing
the metafication properly to pass the reference through).

> $ let $'b = (a = ##\0) + 32'; echo $a $b

For the ## operator the parse is reading just one byte, so you're
still not involving anything that might hit a nul-terminated string.

This is the math parser and only the math parser, but again with
proper metafication:

% let $'a\0b = 2'
zsh: bad math expression: illegal character: \M-C
% let $'ab \0 2'
zsh: bad math expression: illegal character: \M-C
% let $'ab \02'
zsh: bad math expression: illegal character: ^B
% let $'ab y\02'
zsh: bad math expression: operator expected at `y^B'
% let $'ab = \02'
zsh: bad math expression: operand expected at `^B'

I'm not sure it's actually very useful to display more than the first
character in the latter two cases, but we can try to improve what's
currently displayed in multibyte cases.

Compare math parser without metafication:

% a=42
% printf "%d\n" $'a\0b = 2'
42

This is what I meant by "stops at NUL".

> With and without your example patch applied however:
>
> $ printf '%d\n' $'a[\0]++'
> zsh: invalid subscript

That does demonstrate that the length is needed in bin_print() to
properly metafy the subscript, yes.


  parent reply	other threads:[~2024-02-24 21:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-23 23:23 Bart Schaefer
2024-02-24 14:40 ` Stephane Chazelas
2024-02-24 15:10   ` Stephane Chazelas
2024-02-24 21:51   ` Bart Schaefer [this message]
2024-02-25  6:54   ` Stephane Chazelas
2024-02-26  0:04     ` Bart Schaefer
2024-02-26 17:12       ` Bart Schaefer

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=7aYkchMeJX9+jXY1Ev2wDuDAqqAq=DKRG110xOs+UFWxw@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).