zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: unset arbitrary associative array element
Date: Sat, 11 Aug 2018 12:22:26 +0100	[thread overview]
Message-ID: <20180811112226.tiscidxlc7edxqmg@chaz.gmail.com> (raw)

Hi,

one can set an arbitrary associative array element with:

typeset -A hash
hash[$key]=$value

But:

unset "hash[$key]"

Doesn't work for values of $key that contain "]" or "\" (or
characters whose encoding contain the bytes 0x5c or 0x5d) or for
an empty $key.

hash[$key]=()

doesn't work ("attempt to set slice of associative array"
error).

unset "hash[${key//(#m)[]\\]/\\$MATCH}]"

addresses keys with "]" and "\" but not those with characters
whose encoding contains 0x5c/5d or the empty key.

Is there any other way, other than recreating the full array
with something like:

hash=("${(@kv)hash[(I)^$key]}") # untested

ksh93 has the same kind of issues (plus various bugs for
characters that contain 0x5c/5d bytes), but supports unset
"hash[]" (even though like zsh it doesn't support hash[]=value).

In bash, one can do:

unset 'hash[$key]'

That is, the argument is evaluated as shell code! That means we
can unset arbitrary elements that way (though note that bash
doesn't support hash elements with empty keys!), but I would not
want to go there for bash as that means that things like

unset "hash[$key]"

are command injection vulnerabilities.

Not sure what's the best way to address it. Maybe like for
typeset and co, make "unset" a keyword where in

unset hash[$key]

That a[$key] is parsed the same way as in

hash[$key]=value

Or maybe a:

unset -k "$key" hash

Or

hash+=("$key") # add element without a value removes the element

In any case, it would be useful if we could set or unset the
element with the empty key with:

hash[]=value
unset "hash[]"

-- 
Stephane


             reply	other threads:[~2018-08-11 11:22 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-11 11:22 Stephane Chazelas [this message]
2018-08-11 14:55 ` Sebastian Gniazdowski
2018-08-12  3:38   ` Sebastian Gniazdowski
2018-08-12  6:06   ` Stephane Chazelas

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=20180811112226.tiscidxlc7edxqmg@chaz.gmail.com \
    --to=stephane.chazelas@gmail.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).