]> git.ipfire.org Git - thirdparty/bash.git/commitdiff
fix tilde expansion in associative array compound assignments
authorChet Ramey <chet.ramey@case.edu>
Tue, 7 Feb 2023 15:16:51 +0000 (10:16 -0500)
committerChet Ramey <chet.ramey@case.edu>
Tue, 7 Feb 2023 15:16:51 +0000 (10:16 -0500)
CWRU/CWRU.chlog
MANIFEST
arrayfunc.c
subst.c
tests/array.right
tests/array.tests
tests/array31.sub [new file with mode: 0644]
tests/assoc.right
tests/assoc.tests
tests/assoc19.sub [new file with mode: 0644]

index 88185633dae1eb4ef2ccd10860f7e1f9ad9e8ef3..76ba92e9abe943437d2bac62409dbd0c5dd45876 100644 (file)
@@ -5251,3 +5251,19 @@ execute_cmd.c
 print_cmd.c
        - print_function_def,named_function_string: pass POSIXLY_CORRECT to
          valid_function_name as the FLAGS argument
+
+                                   2/6
+                                   ---
+subst.c
+       - expand_subscript_string: don't set W_NOTILDE in td.flags so we
+         perform `normal' tilde expansion (expansion at the start of the
+         word) on the array subscript
+       - expand_oneword,expand_compound_assignment_word: change second arg to
+         ATYPE, which better reflects its use
+
+arrayfunc.c
+       - assign_assoc_from_kvlist,assign_compound_array_list,expand_and_quote_assoc_word:
+         use expand_assignment_string_to_string instead of expand_subscript_string
+         for the rhs of the assignment or the second word in the kvpair to
+         be more consistent with how assignment statements behave (e.g.,
+         tilde expansion). Fixes issue reported by maroloccio@gmail.com
index e5bbafb17eacfbf9412c3a2216ada25544f09c51..f9dc0e9028cb13e3ebecdf672ef4b558c35f11f2 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -942,6 +942,7 @@ tests/array27.sub   f
 tests/array28.sub      f
 tests/array29.sub      f
 tests/array30.sub      f
+tests/array31.sub      f
 tests/array-at-star    f
 tests/array2.right     f
 tests/assoc.tests      f
@@ -964,7 +965,8 @@ tests/assoc15.sub   f
 tests/assoc16.sub      f
 tests/assoc17.sub      f
 tests/assoc18.sub      f
-tests/attr.tests       f
+tests/assoc19.sub      f
+`tests/attr.tests      f
 tests/attr.right       f
 tests/attr1.sub                f
 tests/attr2.sub                f
index 543663ad89ebaf7df3d2e58a3523c54afa46f866..27d84d9b8d19e80868f057a4320209b556da1283 100644 (file)
@@ -597,7 +597,7 @@ assign_assoc_from_kvlist (SHELL_VAR *var, WORD_LIST *nlist, HASH_TABLE *h, int f
          continue;
        }             
 
-      aval = expand_subscript_string (v, 0);
+      aval = expand_assignment_string_to_string (v, 0);
       if (aval == 0)
        {
          aval = (char *)xmalloc (1);
@@ -784,7 +784,7 @@ assign_compound_array_list (SHELL_VAR *var, WORD_LIST *nlist, int flags)
       /* See above; we need to expand the value here */
       if (assoc_p (var))
        {
-         val = expand_subscript_string (val, 0);
+         val = expand_assignment_string_to_string (val, 0);
          if (val == 0)
            {
              val = (char *)xmalloc (1);
@@ -964,7 +964,8 @@ expand_and_quote_assoc_word (char *w, int type)
     nword[i++] = w[ind++];
   nword[i++] = w[ind++];
 
-  t = expand_subscript_string (w+ind, 0);
+
+  t = expand_assignment_string_to_string (w+ind, 0);
   s = (t && strchr (t, CTLESC)) ? quote_escapes (t) : t;
   value = sh_single_quote (s ? s : "");
   if (s != t)
diff --git a/subst.c b/subst.c
index 8c1d94a2cd0fea54209766381f0d38113e55d94f..c7545f6281773276dffa2d4e88c8c21bed905b0f 100644 (file)
--- a/subst.c
+++ b/subst.c
@@ -10518,7 +10518,11 @@ expand_subscript_string (const char *string, int quoted)
   oe = expand_no_split_dollar_star;
   ret = (char *)NULL;
 
+#if 0
   td.flags = W_NOPROCSUB|W_NOTILDE|W_NOSPLIT2; /* XXX - W_NOCOMSUB? */
+#else
+  td.flags = W_NOPROCSUB|W_NOSPLIT2;   /* XXX - W_NOCOMSUB? */
+#endif
   td.word = savestring (string);               /* in case it's freed on error */
 
   expand_no_split_dollar_star = 1;
@@ -10587,6 +10591,7 @@ expand_array_subscript (const char *string, int *sindex, int quoted, int flags)
   exp = substring (string, si+1, ni);
   t = expand_subscript_string (exp, quoted & ~(Q_ARITH|Q_DOUBLE_QUOTES));
   free (exp);
+  /* XXX - use sh_single_quote here? */
   exp = t ? sh_backslash_quote (t, abstab, 0) : savestring ("");
   free (t);
 
@@ -12214,19 +12219,19 @@ make_internal_declare (char *word, char *option, char *cmd)
    ['expanded-ind']='expanded-value'. */
 
 static WORD_LIST *
-expand_oneword (char *value, int flags)
+expand_oneword (char *value, int atype)
 {
   WORD_LIST *l, *nl;
   char *t;
   int kvpair;
   
-  if (flags == 0)
+  if (atype == 0)
     {
       /* Indexed array */
-      l = expand_compound_array_assignment ((SHELL_VAR *)NULL, value, flags);
+      l = expand_compound_array_assignment ((SHELL_VAR *)NULL, value, atype);
       /* Now we quote the results of the expansion above to prevent double
         expansion. */
-      quote_compound_array_list (l, flags);
+      quote_compound_array_list (l, atype);
       return l;
     }
   else
@@ -12251,7 +12256,7 @@ expand_oneword (char *value, int flags)
          if ((nl->word->flags & W_ASSIGNMENT) == 0)
            t = sh_single_quote (nl->word->word ? nl->word->word : "");
          else
-           t = expand_and_quote_assoc_word (nl->word->word, flags);
+           t = expand_and_quote_assoc_word (nl->word->word, atype);
          free (nl->word->word);
          nl->word->word = t;
        }
@@ -12267,7 +12272,7 @@ expand_oneword (char *value, int flags)
    handling, see expand_oneword() above. The return value is
    NAME[+]=( expanded-and-quoted-VALUE ). */
 static void
-expand_compound_assignment_word (WORD_LIST *tlist, int flags)
+expand_compound_assignment_word (WORD_LIST *tlist, int atype)
 {
   WORD_LIST *l;
   int wlen, oind, t;
@@ -12281,7 +12286,7 @@ expand_compound_assignment_word (WORD_LIST *tlist, int flags)
   value = extract_array_assignment_list (tlist->word->word + t + 1, &oind);
   /* This performs one round of expansion on the index/key and value and
      single-quotes each word in the result. */
-  l = expand_oneword (value, flags);
+  l = expand_oneword (value, atype);
   free (value);
 
   value = string_list (l);
index a5cdea88bf5c278fa6a8abae31c24b963e022940..af13502a6faccb14aa26446ff7e99868a0fba901 100644 (file)
@@ -787,3 +787,9 @@ FOO
 declare -Au A=([Darwin]="FOO" )
 FOO
 declare -Au A=(["@"]="FOO" )
+declare -a aa=([0]="/homes/cj/Desktop")
+declare -a aa=([0]="/homes/cj/Desktop")
+declare -a aa=([0]="/homes/cj/Desktop:/homes/cj/Library:/homes/cj/Documents")
+declare -a aa=([0]="/homes/cj/Desktop:/homes/cj/Library:/homes/cj/Documents")
+declare -a aa=([0]="/homes/cj/Desktop:/homes/cj/Library:/homes/cj/Documents" [1]="/homes/cj/Applications")
+declare -a aa=([0]="/homes/cj/Desktop:/homes/cj/Library:/homes/cj/Documents" [1]="/homes/cj/Applications")
index d0bb08b74466ef0e83e420faa8209c713aa7e0a3..6bb256f3de3216ca009f63ce2675802ce9409ee3 100644 (file)
@@ -427,3 +427,4 @@ ${THIS_SH} ./array27.sub
 ${THIS_SH} ./array28.sub
 ${THIS_SH} ./array29.sub
 ${THIS_SH} ./array30.sub
+${THIS_SH} ./array31.sub
diff --git a/tests/array31.sub b/tests/array31.sub
new file mode 100644 (file)
index 0000000..c1ea784
--- /dev/null
@@ -0,0 +1,43 @@
+#   This program is free software: you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation, either version 3 of the License, or
+#   (at your option) any later version.
+#
+#   This program is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License
+#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+# Tilde expansion in indexed array assignments; prep for future work
+HOME=/homes/cj
+
+declare -a aa
+aa=([0]=~/Desktop)
+declare -p aa
+unset aa
+
+declare -a aa=([0]=~/Desktop)
+declare -p aa
+unset aa
+
+declare -a aa=([0]=~/Desktop:~/Library:~/Documents)
+declare -p aa
+unset aa
+
+declare -a aa
+aa=([0]=~/Desktop:~/Library:~/Documents)
+declare -p aa
+unset aa
+
+declare -a aa=([0]=~/Desktop:~/Library:~/Documents [1]=~/Applications)
+declare -p aa
+unset aa
+
+declare -a aa
+aa=([0]=~/Desktop:~/Library:~/Documents [1]=~/Applications)
+declare -p aa
+unset aa
index 9a1662c453376e8ff7e3b05e1c74cab1fdd608c0..bb4a1ba576c26daa48497f7da722b3e015d28835 100644 (file)
@@ -398,3 +398,10 @@ declare -A A=()
 declare -A A=(["]"]="rbracket" ["["]="lbracket" )
 declare -A A=()
 5: ok 1
+declare -A aa=([key]="/homes/cj/Desktop" )
+declare -A aa=([key]="/homes/cj/Desktop" )
+declare -A aa=([k2]="/homes/cj/Library" [key]="/homes/cj/Desktop" )
+declare -A aa=([/homes/cj/key]="/homes/cj/Desktop" )
+declare -A aa=([/homes/cj/Documents]="/homes/cj/Library" [/homes/cj/key]="/homes/cj/Desktop" )
+declare -A aa=([/homes/cj/Documents]="/homes/cj/Library" [/homes/cj/key]="/homes/cj/Desktop:/homes/cj/Documents:/homes/cj/Applications" )
+declare -A aa=([/homes/cj/Documents]="/homes/cj/Library" [/homes/cj/key]="/homes/cj/Desktop:/homes/cj/Documents:/homes/cj/Applications" )
index c656536b5fadc5c8dba59a877dc62605455500f7..5055d5ca085a495fcd72c5cd952283c5ebf61c58 100644 (file)
@@ -266,3 +266,5 @@ ${THIS_SH} ./assoc17.sub
 # tests with `[' and `]' subscripts and printf/read/wait builtins
 ${THIS_SH} ./assoc18.sub
 
+# tests with tilde expansion in keys and values post-bash-5.2
+${THIS_SH} ./assoc19.sub
diff --git a/tests/assoc19.sub b/tests/assoc19.sub
new file mode 100644 (file)
index 0000000..075bccc
--- /dev/null
@@ -0,0 +1,50 @@
+#   This program is free software: you can redistribute it and/or modify
+#   it under the terms of the GNU General Public License as published by
+#   the Free Software Foundation, either version 3 of the License, or
+#   (at your option) any later version.
+#
+#   This program is distributed in the hope that it will be useful,
+#   but WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#   GNU General Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License
+#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+# Problems with tilde expansion of keys and values in bash-5.2
+
+HOME=/homes/cj
+
+declare -A aa=([key]=~/Desktop) 
+declare -p aa
+unset aa
+
+declare -A aa
+aa=([key]=~/Desktop) 
+declare -p aa
+unset aa
+
+declare -A aa=([key]=~/Desktop [k2]=~/Library )
+declare -p aa
+unset aa
+
+declare -A aa=([~/key]=~/Desktop) 
+declare -p aa
+unset aa
+
+declare -A aa
+aa=([~/key]=~/Desktop) 
+aa[~/Documents]=~/Library
+declare -p aa
+unset aa
+
+declare -A aa
+aa=([~/key]=~/Desktop:~/Documents:~/Applications) 
+aa[~/Documents]=~/Library
+declare -p aa
+unset aa
+
+declare -A aa=([~/key]=~/Desktop:~/Documents:~/Applications [~/Documents]=~/Library)
+declare -p aa
+
+