b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | BASH PATCH REPORT |
| 2 | ================= |
| 3 | |
| 4 | Bash-Release: 5.1 |
| 5 | Patch-ID: bash51-004 |
| 6 | |
| 7 | Bug-Reported-by: oguzismailuysal@gmail.com |
| 8 | Bug-Reference-ID: <CAH7i3LoHGmwaghDpCWRUfcY04gQmeDTH3RiG=bf2b=KbU=gyhw@mail.gmail.com> |
| 9 | Bug-Reference-URL: https://lists.gnu.org/archive/html/bug-bash/2020-12/msg00039.html |
| 10 | |
| 11 | Bug-Description: |
| 12 | |
| 13 | If a key-value compound array assignment to an associative array is supplied |
| 14 | as an assignment statement argument to the `declare' command that declares the |
| 15 | array, the assignment doesn't perform the correct word expansions. |
| 16 | |
| 17 | This patch makes key-value assignment and subscript assignment perform the |
| 18 | same expansions when they're supplied as an argument to `declare'. |
| 19 | |
| 20 | Patch (apply with `patch -p0'): |
| 21 | |
| 22 | --- a/arrayfunc.c |
| 23 | +++ b/arrayfunc.c |
| 24 | @@ -597,6 +597,27 @@ assign_assoc_from_kvlist (var, nlist, h, |
| 25 | free (aval); |
| 26 | } |
| 27 | } |
| 28 | + |
| 29 | +/* Return non-zero if L appears to be a key-value pair associative array |
| 30 | + compound assignment. */ |
| 31 | +int |
| 32 | +kvpair_assignment_p (l) |
| 33 | + WORD_LIST *l; |
| 34 | +{ |
| 35 | + return (l && (l->word->flags & W_ASSIGNMENT) == 0 && l->word->word[0] != '['); /*]*/ |
| 36 | +} |
| 37 | + |
| 38 | +char * |
| 39 | +expand_and_quote_kvpair_word (w) |
| 40 | + char *w; |
| 41 | +{ |
| 42 | + char *t, *r; |
| 43 | + |
| 44 | + t = w ? expand_assignment_string_to_string (w, 0) : 0; |
| 45 | + r = sh_single_quote (t ? t : ""); |
| 46 | + free (t); |
| 47 | + return r; |
| 48 | +} |
| 49 | #endif |
| 50 | |
| 51 | /* Callers ensure that VAR is not NULL. Associative array assignments have not |
| 52 | @@ -640,7 +661,7 @@ assign_compound_array_list (var, nlist, |
| 53 | last_ind = (a && (flags & ASS_APPEND)) ? array_max_index (a) + 1 : 0; |
| 54 | |
| 55 | #if ASSOC_KVPAIR_ASSIGNMENT |
| 56 | - if (assoc_p (var) && nlist && (nlist->word->flags & W_ASSIGNMENT) == 0 && nlist->word->word[0] != '[') /*]*/ |
| 57 | + if (assoc_p (var) && kvpair_assignment_p (nlist)) |
| 58 | { |
| 59 | iflags = flags & ~ASS_APPEND; |
| 60 | assign_assoc_from_kvlist (var, nlist, nhash, iflags); |
| 61 | --- a/arrayfunc.h |
| 62 | +++ b/arrayfunc.h |
| 63 | @@ -67,6 +67,9 @@ extern SHELL_VAR *assign_array_var_from_ |
| 64 | extern char *expand_and_quote_assoc_word PARAMS((char *, int)); |
| 65 | extern void quote_compound_array_list PARAMS((WORD_LIST *, int)); |
| 66 | |
| 67 | +extern int kvpair_assignment_p PARAMS((WORD_LIST *)); |
| 68 | +extern char *expand_and_quote_kvpair_word PARAMS((char *)); |
| 69 | + |
| 70 | extern int unbind_array_element PARAMS((SHELL_VAR *, char *, int)); |
| 71 | extern int skipsubscript PARAMS((const char *, int, int)); |
| 72 | |
| 73 | --- a/subst.c |
| 74 | +++ b/subst.c |
| 75 | @@ -11604,6 +11604,7 @@ expand_oneword (value, flags) |
| 76 | { |
| 77 | WORD_LIST *l, *nl; |
| 78 | char *t; |
| 79 | + int kvpair; |
| 80 | |
| 81 | if (flags == 0) |
| 82 | { |
| 83 | @@ -11618,11 +11619,21 @@ expand_oneword (value, flags) |
| 84 | { |
| 85 | /* Associative array */ |
| 86 | l = parse_string_to_word_list (value, 1, "array assign"); |
| 87 | +#if ASSOC_KVPAIR_ASSIGNMENT |
| 88 | + kvpair = kvpair_assignment_p (l); |
| 89 | +#endif |
| 90 | + |
| 91 | /* For associative arrays, with their arbitrary subscripts, we have to |
| 92 | expand and quote in one step so we don't have to search for the |
| 93 | closing right bracket more than once. */ |
| 94 | for (nl = l; nl; nl = nl->next) |
| 95 | { |
| 96 | +#if ASSOC_KVPAIR_ASSIGNMENT |
| 97 | + if (kvpair) |
| 98 | + /* keys and values undergo the same set of expansions */ |
| 99 | + t = expand_and_quote_kvpair_word (nl->word->word); |
| 100 | + else |
| 101 | +#endif |
| 102 | if ((nl->word->flags & W_ASSIGNMENT) == 0) |
| 103 | t = sh_single_quote (nl->word->word ? nl->word->word : ""); |
| 104 | else |
| 105 | --- a/patchlevel.h |
| 106 | +++ b/patchlevel.h |
| 107 | @@ -25,6 +25,6 @@ |
| 108 | regexp `^#define[ ]*PATCHLEVEL', since that's what support/mkversion.sh |
| 109 | looks for to find the patch level (for the sccs version string). */ |
| 110 | |
| 111 | -#define PATCHLEVEL 3 |
| 112 | +#define PATCHLEVEL 4 |
| 113 | |
| 114 | #endif /* _PATCHLEVEL_H_ */ |