Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: [PATCH] (?) typeset array[position=index]=value
- X-seq: zsh-workers 48983
- From: Stephane Chazelas <stephane@xxxxxxxxxxxx>
- To: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- Cc: Zsh hackers list <zsh-workers@xxxxxxx>
- Subject: Re: [PATCH] (?) typeset array[position=index]=value
- Date: Wed, 2 Jun 2021 10:11:45 +0100
- Archived-at: <https://zsh.org/workers/48983>
- In-reply-to: <CAH+w=7a+M7nTTqzmve+SZHwtEeRPTicQB5ZRy2XO6er4UhMb1w@mail.gmail.com>
- List-id: <zsh-workers.zsh.org>
- Mail-followup-to: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>, Zsh hackers list <zsh-workers@xxxxxxx>
- References: <20191217111113.z242f4g6sx7xdwru@chaz.gmail.com> <2ea6feb3-a686-4d83-ab27-6a582424487c@www.fastmail.com> <20200101140343.qwfx2xaojumuds3d@chaz.gmail.com> <20210430061117.buyhdhky5crqjrf2@chazelas.org> <CAH+w=7bHxSbFr60ZU0+oZ6+qEejhfBYTzvL7=aXadY5XzWtSzw@mail.gmail.com> <20210505114521.bemoiekpophssbug@chazelas.org> <CAH+w=7ZqE2DnxpHhvCjZnXB4A1vJ=EKB2fpWyUMaZX0VYqU9kg@mail.gmail.com> <CAH+w=7bVn2LiTsq194GjshVSOCSib7t4T=uZQ2ZvUGu6Z2XoPw@mail.gmail.com> <20210601053235.b4junj6muuwegl7b@chazelas.org> <CAH+w=7a+M7nTTqzmve+SZHwtEeRPTicQB5ZRy2XO6er4UhMb1w@mail.gmail.com>
2021-06-01 09:05:13 -0700, Bart Schaefer:
[...]
> > [...] unless we're happy to break backward
> > compatibility and make unset "assoc[$key]" work whatever the
> > value of $key (unset 'assoc[f\]oo]]' for unset the element of
> > key 'f\]oo]' for instance)
>
> Hm, I'm not sure what backward-compatibility would be broken? Do you
> mean that scripts that already have work-arounds for the current issue
> might stop working?
Yes, like that guy's unset_keys at
https://unix.stackexchange.com/questions/626393/in-zsh-how-do-i-unset-an-arbitrary-associative-array-element/626529#626529
mentioned earlier.
ATM, you need to do:
unset 'hash[\]]'
To unset the element of key "]" for instance.
Having said that, in practice, it's rare for values to be passed
literally. What you generally want to do is:
unset "hash[$key]"
With $key being any arbitrary string. And that's where the
problem is.
Maybe the best approach would be to make unset a dual
keyword/builtin like typeset/export... so one can do:
unset hash[$key]
And that hash[$key] being interpreted the same as when you do:
hash[$key]=value
Being able to do:
unset hash[(R)pattern]
unset hash[(I)pattern]
would also be useful. We don't want however subscript flags to
be interpreted in:
unset "hash[$key]"
as that would introduce command injection vulnerabilities, like
is already the case in things like:
$ key='(n:evil:)' evil='psvar[$(uname>&2)1]' zsh -c 'typeset -A a; (( a[$key]++ ))'
Linux
Linux
(though in that case, that's not limited to subscript flags,
key='x]+psvar[$(uname>&2)1' works as well, see
https://unix.stackexchange.com/questions/627474/how-to-use-associative-arrays-safely-inside-arithmetic-expressions/627475#627475
)
That's also why I was suggesting allowing:
hash[$key]=()
to unset an element, where you don't have the ambiguity of
whether the contents of $key is going to be interpreted
specially.
--
Stephane
Messages sorted by:
Reverse Date,
Date,
Thread,
Author