about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBart Schaefer <schaefer@ipost.com>2021-06-13 16:30:06 -0700
committerBart Schaefer <schaefer@ipost.com>2021-06-13 16:30:06 -0700
commit5b4a1626c2efbbc4b46e8995e553ec2c14549904 (patch)
treed3ee7a205eb0013e2f58e26ec75cad58e3628752
parent667901b6ba8ea7871bfdb6364fb14cbcb70536b4 (diff)
downloadzsh-5b4a1626c2efbbc4b46e8995e553ec2c14549904.tar.gz
zsh-5b4a1626c2efbbc4b46e8995e553ec2c14549904.tar.xz
zsh-5b4a1626c2efbbc4b46e8995e553ec2c14549904.zip
49069: literal interpretation of subscripts for unset of array/hash elements
-rw-r--r--ChangeLog5
-rw-r--r--NEWS12
-rw-r--r--Src/builtin.c6
3 files changed, 19 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 78271d448..83c2ac12d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2021-06-13  Bart Schaefer  <schaefer@zsh.org>
+
+	* 49069: NEWS, Src/builtin.c: literal interpretation of subscripts
+	for unset of array/hash elements
+
 2021-06-02  Oliver Kiddle  <opk@zsh.org>
 
 	* Akinori MUSHA: 48942: Functions/Zle/edit-command-line:
diff --git a/NEWS b/NEWS
index ee97868f9..c12ec3b0e 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,18 @@ Note also the list of incompatibilities in the README file.
 Changes since 5.8
 -----------------
 
+When unsetting a hash element, the string enclosed in square brackets is
+interpreted literally after any normal command-line-argument expansions.
+Thus
+  unset "hash[$key]"
+first expands $key as usual for a double-quoted string, and then interprets
+that result as the exact hash element to unset.  This differs from previous
+versions of the shell, which would also remove a leading backslash for an
+unusual subset of characters in the expansion of $key.  Note this also
+means, for example, that
+  unset 'hash[ab]cd]'
+unsets the element with key "ab]cd" rather than silently doing nothing.
+
 The function command learnt a -T option to declare a function and enable
 tracing for it simultaneously.
 
diff --git a/Src/builtin.c b/Src/builtin.c
index a16fddcb7..d7d2ea297 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -3724,14 +3724,12 @@ bin_unset(char *name, char **argv, Options ops, int func)
     while ((s = *argv++)) {
 	char *ss = strchr(s, '['), *subscript = 0;
 	if (ss) {
-	    char *sse;
+	    char *sse = ss + strlen(ss)-1;
 	    *ss = 0;
-	    if ((sse = parse_subscript(ss+1, 1, ']'))) {
+	    if (*sse == ']') {
 		*sse = 0;
 		subscript = dupstring(ss+1);
 		*sse = ']';
-		remnulargs(subscript);
-		untokenize(subscript);
 	    }
 	}
 	if ((ss && !subscript) || !isident(s)) {