]> git.ipfire.org Git - thirdparty/bash.git/commitdiff
Bash-5.1 patch 4: fix key-value pair associative array assignment word expansions
authorChet Ramey <chet.ramey@case.edu>
Sat, 19 Dec 2020 19:30:13 +0000 (14:30 -0500)
committerChet Ramey <chet.ramey@case.edu>
Sat, 19 Dec 2020 19:30:13 +0000 (14:30 -0500)
arrayfunc.c
arrayfunc.h
patchlevel.h
subst.c

index 2f8a38484d253bf92576e87e7003a39a946427bc..8231ba1eded7d704a4fc2067c24d9af4b59d07e4 100644 (file)
@@ -597,6 +597,27 @@ assign_assoc_from_kvlist (var, nlist, h, flags)
        free (aval);
     }
 }
+
+/* Return non-zero if L appears to be a key-value pair associative array
+   compound assignment. */ 
+int
+kvpair_assignment_p (l)
+     WORD_LIST *l;
+{
+  return (l && (l->word->flags & W_ASSIGNMENT) == 0 && l->word->word[0] != '[');       /*]*/
+}
+
+char *
+expand_and_quote_kvpair_word (w)
+     char *w;
+{
+  char *t, *r;
+
+  t = w ? expand_assignment_string_to_string (w, 0) : 0;
+  r = sh_single_quote (t ? t : "");
+  free (t);
+  return r;
+}
 #endif
      
 /* Callers ensure that VAR is not NULL. Associative array assignments have not
@@ -640,7 +661,7 @@ assign_compound_array_list (var, nlist, flags)
   last_ind = (a && (flags & ASS_APPEND)) ? array_max_index (a) + 1 : 0;
 
 #if ASSOC_KVPAIR_ASSIGNMENT
-  if (assoc_p (var) && nlist && (nlist->word->flags & W_ASSIGNMENT) == 0 && nlist->word->word[0] != '[')       /*]*/
+  if (assoc_p (var) && kvpair_assignment_p (nlist))
     {
       iflags = flags & ~ASS_APPEND;
       assign_assoc_from_kvlist (var, nlist, nhash, iflags);
index c2bbb98a66586514197b0fb5fef3116a40535146..838e76d2d484a08322f6544fa5d8462e0f2eb0b1 100644 (file)
@@ -67,6 +67,9 @@ extern SHELL_VAR *assign_array_var_from_string PARAMS((SHELL_VAR *, char *, int)
 extern char *expand_and_quote_assoc_word PARAMS((char *, int));
 extern void quote_compound_array_list PARAMS((WORD_LIST *, int));
 
+extern int kvpair_assignment_p PARAMS((WORD_LIST *));
+extern char *expand_and_quote_kvpair_word PARAMS((char *));
+
 extern int unbind_array_element PARAMS((SHELL_VAR *, char *, int));
 extern int skipsubscript PARAMS((const char *, int, int));
 
index e4d85cf8b5c232793fb9255ad7c74a0f289fe6b8..e1429c245ffb845f2c7ae586bc54b86f5af9d038 100644 (file)
@@ -25,6 +25,6 @@
    regexp `^#define[   ]*PATCHLEVEL', since that's what support/mkversion.sh
    looks for to find the patch level (for the sccs version string). */
 
-#define PATCHLEVEL 3
+#define PATCHLEVEL 4
 
 #endif /* _PATCHLEVEL_H_ */
diff --git a/subst.c b/subst.c
index 23f847f41625ac430d143a10b529973002615869..6132316a5ce0b774727b565b373f68b5a2562ea4 100644 (file)
--- a/subst.c
+++ b/subst.c
@@ -11604,6 +11604,7 @@ expand_oneword (value, flags)
 {
   WORD_LIST *l, *nl;
   char *t;
+  int kvpair;
   
   if (flags == 0)
     {
@@ -11618,11 +11619,21 @@ expand_oneword (value, flags)
     {
       /* Associative array */
       l = parse_string_to_word_list (value, 1, "array assign");
+#if ASSOC_KVPAIR_ASSIGNMENT
+      kvpair = kvpair_assignment_p (l);
+#endif
+
       /* For associative arrays, with their arbitrary subscripts, we have to
         expand and quote in one step so we don't have to search for the
         closing right bracket more than once. */
       for (nl = l; nl; nl = nl->next)
        {
+#if ASSOC_KVPAIR_ASSIGNMENT
+         if (kvpair)
+           /* keys and values undergo the same set of expansions */
+           t = expand_and_quote_kvpair_word (nl->word->word);
+         else
+#endif
          if ((nl->word->flags & W_ASSIGNMENT) == 0)
            t = sh_single_quote (nl->word->word ? nl->word->word : "");
          else