BASH PATCH REPORT ================= Bash-Release: 5.2 Patch-ID: bash52-007 Bug-Reported-by: Bruce Jerrick Bug-Reference-ID: Bug-Reference-URL: https://bugzilla.redhat.com/show_bug.cgi?id=2134307 Bug-Description: This patch fixes several problems with alias expansion inside command substitutions when in POSIX mode. --- a/builtins/evalstring.c +++ b/builtins/evalstring.c @@ -431,6 +431,8 @@ parse_and_execute (string, from_file, fl if (parse_command () == 0) { + int local_expalias, local_alflag; + if ((flags & SEVAL_PARSEONLY) || (interactive_shell == 0 && read_but_dont_execute)) { last_result = EXECUTION_SUCCESS; @@ -507,6 +509,19 @@ parse_and_execute (string, from_file, fl } #endif /* ONESHOT */ + /* We play tricks in the parser and command_substitute() turning + expand_aliases on and off depending on which parsing pass and + whether or not we're in posix mode. This only matters for + parsing, and we let the higher layers deal with that. We just + want to ensure that expand_aliases is set to the appropriate + global value when we go to execute this command, so we save + and restore it around the execution (we don't restore it if + the global value of the flag (expaliases_flag) changes). */ + local_expalias = expand_aliases; + local_alflag = expaliases_flag; + if (subshell_environment & SUBSHELL_COMSUB) + expand_aliases = expaliases_flag; + /* See if this is a candidate for $( word : (char *)NULL; if (tword) dispose_word_desc (tword); --- a/y.tab.c +++ b/y.tab.c @@ -5923,6 +5923,7 @@ tokword: #define P_BACKQUOTE 0x0010 /* parsing a backquoted command substitution */ #define P_ARRAYSUB 0x0020 /* parsing a [...] array subscript for assignment */ #define P_DOLBRACE 0x0040 /* parsing a ${...} construct */ +#define P_ARITH 0x0080 /* parsing a $(( )) arithmetic expansion */ /* Lexical state while parsing a grouping construct or $(...). */ #define LEX_WASDOL 0x0001 @@ -6221,6 +6222,9 @@ parse_matched_pair (qc, open, close, len } else if ((flags & (P_ARRAYSUB|P_DOLBRACE)) && (tflags & LEX_WASDOL) && (ch == '(' || ch == '{' || ch == '[')) /* ) } ] */ goto parse_dollar_word; + else if ((flags & P_ARITH) && (tflags & LEX_WASDOL) && ch == '(') /*)*/ + /* $() inside $(( ))/$[ ] */ + goto parse_dollar_word; #if defined (PROCESS_SUBSTITUTION) /* XXX - technically this should only be recognized at the start of a word */ @@ -6251,7 +6255,7 @@ parse_dollar_word: else if (ch == '{') /* } */ nestret = parse_matched_pair (0, '{', '}', &nestlen, P_FIRSTCLOSE|P_DOLBRACE|rflags); else if (ch == '[') /* ] */ - nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags); + nestret = parse_matched_pair (0, '[', ']', &nestlen, rflags|P_ARITH); CHECK_NESTRET_ERROR (); APPEND_NESTRET (); @@ -6390,7 +6394,7 @@ parse_comsub (qc, open, close, lenp, fla peekc = shell_getc (1); shell_ungetc (peekc); if (peekc == '(') /*)*/ - return (parse_matched_pair (qc, open, close, lenp, 0)); + return (parse_matched_pair (qc, open, close, lenp, P_ARITH)); } /*itrace("parse_comsub: qc = `%c' open = %c close = %c", qc, open, close);*/ @@ -6811,7 +6815,7 @@ parse_arith_cmd (ep, adddq) int ttoklen; exp_lineno = line_number; - ttok = parse_matched_pair (0, '(', ')', &ttoklen, 0); + ttok = parse_matched_pair (0, '(', ')', &ttoklen, P_ARITH); rval = 1; if (ttok == &matched_pair_error) return -1; @@ -7326,7 +7330,7 @@ read_token_word (character) pop_delimiter (dstack); } else - ttok = parse_matched_pair (cd, '[', ']', &ttoklen, 0); + ttok = parse_matched_pair (cd, '[', ']', &ttoklen, P_ARITH); if (ttok == &matched_pair_error) return -1; /* Bail immediately. */ RESIZE_MALLOCED_BUFFER (token, token_index, ttoklen + 3,