]> git.ipfire.org Git - thirdparty/bash.git/commitdiff
fixes for extended glob in compat mode; fix for brackets appearing in nested paramete...
authorChet Ramey <chet.ramey@case.edu>
Fri, 7 Oct 2022 21:46:16 +0000 (17:46 -0400)
committerChet Ramey <chet.ramey@case.edu>
Fri, 7 Oct 2022 21:46:16 +0000 (17:46 -0400)
13 files changed:
CWRU/CWRU.chlog
MANIFEST
builtins/common.h
builtins/shopt.def
execute_cmd.c
lib/readline/nls.c
parse.y
patchlevel.h
subst.c
tests/extglob.right
tests/extglob.tests
tests/extglob8.sub [new file with mode: 0644]
tests/run-extglob

index c6db0eb5c3e2dad5bc9254d15d275e46d53026a7..d2f2b8ad0be1e4e7af459effe3825cab70dae727 100644 (file)
@@ -4030,3 +4030,35 @@ lib/readline/nls.c
 
 lib/readline/{input.c,rlprivate.h}
        - fixes for compiling on w64-mingw32
+
+                                  10/5
+                                  ----
+parse.y
+       - xparse_dolparen: save and restore extended_glob using local_extglob
+         in the same way that parse_comsub does. Fixes compat issue reported in
+         https://bugs.gentoo.org/873931 and by Kerin Millar <kfm@plushkava.net>
+
+                                  10/6
+                                  ----
+subst.c
+       - extract_dollar_brace_string: keep a stack of dolbrace_state values
+         to handle nested expansions. Report from
+         Antoine <bug-bash@glitchimini.net>
+
+                                  10/7
+                                  ----
+builtins/{shopt.def,common.h}
+       - extglob_flag: new variable to hold the state of the `extglob' option;
+         initialized to EXTGLOB_DEFAULT like extended_glob
+       - shopt_set_extglob: new function to set extended_glob to the right
+         value for the rest of the shell
+
+parse.y
+       - reset_parser: set extended_glob to extglob_flag, which is only
+         changed by shopt, remove global_extglob
+       - parse_comsub,parse_cond_command,xparse_dolparen: no longer use
+         global_extglob
+
+execute_cmd.c
+       - execute_cond_node: reset extended_glob to the value of extglob_flag,
+         since we're executing a command here
index a45b3cfc27869b495e55faa6d6fdbadbeb6a46d4..07e216514db9d8e56311a57e1734c8ab8cd44fb8 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -1131,6 +1131,7 @@ tests/extglob4.sub        f
 tests/extglob5.sub     f
 tests/extglob6.sub     f
 tests/extglob7.sub     f
+tests/extglob8.sub     f
 tests/func.tests       f
 tests/func.right       f
 tests/func1.sub                f
index f5cd87f081f700f203c27fa315666f1189679024..543e4e0c05113ebe0ff6b3ddf8622ce406c9f5b7 100644 (file)
@@ -257,6 +257,10 @@ extern int print_shift_error;
 extern int expand_once_flag;
 #endif
 
+#if defined (EXTENDED_GLOB)
+extern int extglob_flag;
+#endif
+
 /* variables from source.def */
 extern int source_searches_cwd;
 extern int source_uses_path;
index 33d61d4c5a21e1436d1022eac4bd4bf35204d7bb..0a6a9b743a4463981918c15b452023e34fdcad18 100644 (file)
@@ -1,7 +1,7 @@
 This file is shopt.def, from which is created shopt.c.
 It implements the Bash `shopt' builtin.
 
-Copyright (C) 1994-2021 Free Software Foundation, Inc.
+Copyright (C) 1994-2022 Free Software Foundation, Inc.
 
 This file is part of GNU Bash, the Bourne Again SHell.
 
@@ -149,6 +149,11 @@ static int shopt_set_complete_direxpand PARAMS((char *, int));
 static int set_assoc_expand PARAMS((char *, int));
 #endif
 
+#if defined (EXTENDED_GLOB)
+int extglob_flag = EXTGLOB_DEFAULT;
+static int shopt_set_extglob PARAMS((char *, int));
+#endif
+
 static int shopt_set_debug_mode PARAMS((char *, int));
 
 static int shopt_login_shell;
@@ -203,7 +208,7 @@ static struct {
   { "extdebug", &debugging_mode, shopt_set_debug_mode },
 #endif
 #if defined (EXTENDED_GLOB)
-  { "extglob", &extended_glob, (shopt_set_func_t *)NULL },
+  { "extglob", &extglob_flag, shopt_set_extglob },
 #endif
   { "extquote", &extended_quote, (shopt_set_func_t *)NULL },
   { "failglob", &fail_glob_expansion, (shopt_set_func_t *)NULL },
@@ -374,7 +379,7 @@ reset_shopt_options ()
 #endif
 
 #if defined (EXTENDED_GLOB)
-  extended_glob = EXTGLOB_DEFAULT;
+  extended_glob = extglob_flag = EXTGLOB_DEFAULT;
 #endif
 
 #if defined (ARRAY_VARS)
@@ -631,11 +636,24 @@ shopt_set_debug_mode (option_name, mode)
   return (0);
 }
 
+#if defined (EXTENDED_GLOB)
+static int
+shopt_set_extglob (option_name, mode)
+     char *option_name;
+     int mode;
+{
+  extended_glob = extglob_flag;
+  return 0;
+}
+#endif
+
 #if defined (READLINE)
 static int
 shopt_enable_hostname_completion (option_name, mode)
      char *option_name;
      int mode;
+
+
 {
   return (enable_hostname_completion (mode));
 }
index b62f910508971865519ef23de66fb48e65cd267d..a37890ab37ac9e3fe3a41acfbfebb09388958dc7 100644 (file)
@@ -3993,13 +3993,11 @@ execute_cond_node (cond)
       else
 #endif /* COND_REGEXP */
        {
-         int oe;
-         oe = extended_glob;
          extended_glob = 1;
          result = binary_test (cond->op->word, arg1, arg2, TEST_PATMATCH|TEST_ARITHEXP|TEST_LOCALE)
                                  ? EXECUTION_SUCCESS
                                  : EXECUTION_FAILURE;
-         extended_glob = oe;
+         extended_glob = extglob_flag;
        }
       if (arg1 != nullstr)
        free (arg1);
index f117cf9e6ef9601d61cabcc926725d11bb8516b7..8c027d6be500d06d90a7549e70127c3ba16093a6 100644 (file)
@@ -142,7 +142,7 @@ _rl_init_locale (void)
     lspec = "";
   ret = setlocale (LC_CTYPE, lspec);   /* ok, since it does not change locale */
   if (ret == 0 || *ret == 0)
-    ret = setlocale (LC_CTYPE, NULL);
+    ret = setlocale (LC_CTYPE, (char *)NULL);
   if (ret == 0 || *ret == 0)
     ret = RL_DEFAULT_LOCALE;
 #else
diff --git a/parse.y b/parse.y
index d887eecb6cddba6877952d6f60944e913e626b1b..817ae24b9f894cbf1089153ba68a5c73b9d5f9a0 100644 (file)
--- a/parse.y
+++ b/parse.y
@@ -125,7 +125,7 @@ do { \
 } while (0)
 
 #if defined (EXTENDED_GLOB)
-extern int extended_glob;
+extern int extended_glob, extglob_flag;
 #endif
 
 #if defined (TRANSLATABLE_STRINGS)
@@ -312,8 +312,6 @@ static int token_before_that;
 /* The token read prior to token_before_that. */
 static int two_tokens_ago;
 
-static int global_extglob;
-
 /* The line number in a script where the word in a `case WORD', `select WORD'
    or `for WORD' begins.  This is a nested command maximum, since the array
    index is decremented after a case, select, or for command is parsed. */
@@ -3304,7 +3302,7 @@ reset_parser ()
 #if defined (EXTENDED_GLOB)
   /* Reset to global value of extended glob */
   if (parser_state & (PST_EXTPAT|PST_CMDSUBST))
-    extended_glob = global_extglob;
+    extended_glob = extglob_flag;
 #endif
 
   parser_state = 0;
@@ -4118,10 +4116,10 @@ parse_comsub (qc, open, close, lenp, flags)
     expand_aliases = posixly_correct != 0;
 #if defined (EXTENDED_GLOB)
   /* If (parser_state & PST_EXTPAT), we're parsing an extended pattern for a
-     conditional command and have already set global_extglob appropriately. */
+     conditional command and have already set extended_glob appropriately. */
   if (shell_compatibility_level <= 51 && was_extpat == 0)
     {
-      local_extglob = global_extglob = extended_glob;
+      local_extglob = extended_glob;
       extended_glob = 1;
     }
 #endif
@@ -4229,7 +4227,7 @@ xparse_dolparen (base, string, indp, flags)
 {
   sh_parser_state_t ps;
   sh_input_line_state_t ls;
-  int orig_ind, nc, sflags, start_lineno;
+  int orig_ind, nc, sflags, start_lineno, local_extglob;
   char *ret, *ep, *ostring;
 
 /*debug_parser(1);*/
@@ -4272,7 +4270,7 @@ xparse_dolparen (base, string, indp, flags)
      old value will be restored by restore_parser_state(). */
   expand_aliases = 0;
 #if defined (EXTENDED_GLOB)
-  global_extglob = extended_glob;              /* for reset_parser() */
+  local_extglob = extended_glob;
 #endif
 
   token_to_read = DOLPAREN;                    /* let's trick the parser */
@@ -4290,6 +4288,9 @@ xparse_dolparen (base, string, indp, flags)
   restore_input_line_state (&ls);
   restore_parser_state (&ps);
 
+#if defined (EXTENDED_GLOB)
+  extended_glob = local_extglob;
+#endif
   token_to_read = 0;
 
   /* If parse_string returns < 0, we need to jump to top level with the
@@ -4724,12 +4725,16 @@ cond_term ()
        }
 
       /* rhs */
+#if defined (EXTENDED_GLOB)
       local_extglob = extended_glob;
       if (parser_state & PST_EXTPAT)
        extended_glob = 1;
+#endif
       tok = read_token (READ);
+#if defined (EXTENDED_GLOB)
       if (parser_state & PST_EXTPAT)
        extended_glob = local_extglob;
+#endif
       parser_state &= ~(PST_REGEXP|PST_EXTPAT);
 
       if (tok == WORD)
@@ -4776,7 +4781,6 @@ parse_cond_command ()
 {
   COND_COM *cexp;
 
-  global_extglob = extended_glob;
   cexp = cond_expr ();
   return (make_cond_command (cexp));
 }
index 165390c155411545a5e05f0ed98d1c85d0098955..0d5a448e9af60f52edc4ae4bf5dacc109881015a 100644 (file)
@@ -1,6 +1,6 @@
 /* patchlevel.h -- current bash patch level */
 
-/* Copyright (C) 2001-2021 Free Software Foundation, Inc.
+/* Copyright (C) 2001-2022 Free Software Foundation, Inc.
 
    This file is part of GNU Bash, the Bourne Again SHell.
 
@@ -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 0
+#define PATCHLEVEL 2
 
 #endif /* _PATCHLEVEL_H_ */
diff --git a/subst.c b/subst.c
index bae6e3a11d52afcae73610d1103de7b70c471b5f..51fd6468f248315fa71792d6c4f058e11f8c5c84 100644 (file)
--- a/subst.c
+++ b/subst.c
@@ -1803,6 +1803,9 @@ extract_heredoc_dolbrace_string (string, sindex, quoted, flags)
   return (result);
 }
 
+#define PARAMEXPNEST_MAX       32      // for now
+static int dbstate[PARAMEXPNEST_MAX];
+
 /* Extract a parameter expansion expression within ${ and } from STRING.
    Obey the Posix.2 rules for finding the ending `}': count braces while
    skipping over enclosed quoted strings and command substitutions.
@@ -1833,6 +1836,8 @@ extract_dollar_brace_string (string, sindex, quoted, flags)
   if (quoted == Q_HERE_DOCUMENT && dolbrace_state == DOLBRACE_QUOTE && (flags & SX_NOALLOC) == 0)
     return (extract_heredoc_dolbrace_string (string, sindex, quoted, flags));
 
+  dbstate[0] = dolbrace_state;
+
   pass_character = 0;
   nesting_level = 1;
   slen = strlen (string + *sindex) + *sindex;
@@ -1857,6 +1862,8 @@ extract_dollar_brace_string (string, sindex, quoted, flags)
 
       if (string[i] == '$' && string[i+1] == LBRACE)
        {
+         if (nesting_level < PARAMEXPNEST_MAX)
+           dbstate[nesting_level] = dolbrace_state;
          nesting_level++;
          i += 2;
          if (dolbrace_state == DOLBRACE_QUOTE || dolbrace_state == DOLBRACE_WORD)
@@ -1869,6 +1876,7 @@ extract_dollar_brace_string (string, sindex, quoted, flags)
          nesting_level--;
          if (nesting_level == 0)
            break;
+         dolbrace_state = (nesting_level < PARAMEXPNEST_MAX) ? dbstate[nesting_level] : dbstate[0];    /* Guess using initial state */
          i++;
          continue;
        }
index 2974cecaf8f974abcccada768528196a5e72440c..dca15ed53b4390ab35293423c2f8db3f3e98c113 100644 (file)
@@ -182,3 +182,10 @@ no dotglob: .a .foo bar
 ? . .. .a .foo
 *
 bar
+extglob                off
+x
+extglob                off
+extglob                off
+extglob                off
+extglob                off
+extglob                off
index 7a7cf91bfb970d77719040621b8351b106ce5964..f13e8fb62287aa3b8f299f01ec67bd0a1e05f4fe 100644 (file)
@@ -386,5 +386,6 @@ ${THIS_SH} ./extglob4.sub
 ${THIS_SH} ./extglob5.sub
 ${THIS_SH} ./extglob6.sub
 ${THIS_SH} ./extglob7.sub
+${THIS_SH} ./extglob8.sub
 
 exit 0
diff --git a/tests/extglob8.sub b/tests/extglob8.sub
new file mode 100644 (file)
index 0000000..6b644bb
--- /dev/null
@@ -0,0 +1,33 @@
+#   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/>.
+#
+# extglob option interaction with other parts of the shell that force
+# extended_glob on; only an issue in compatibility mode
+
+shopt -u extglob
+
+BASH_COMPAT=50
+shopt extglob
+
+echo $(echo $(echo $(echo $(echo $(echo x) ) ) ) )
+shopt extglob
+
+shopt -u extglob
+
+[[ '' = $(shopt extglob >&2) ]]
+shopt extglob
+
+shopt -u extglob
+
+[[ foo = $(: $(shopt extglob >&2)) ]]
+shopt extglob
index 149e34af871206a4a7ea9677981f3b26838ccdd4..a1464aa7e4e37882962f6fd735d20312b5994da6 100644 (file)
@@ -1,4 +1,4 @@
 PATH=$PATH:`pwd`
 export PATH
-${THIS_SH} ./extglob.tests | grep -v '^expect' > ${BASH_TSTOUT}
+${THIS_SH} ./extglob.tests 2>&1 | grep -v '^expect' > ${BASH_TSTOUT}
 diff ${BASH_TSTOUT} extglob.right && rm -f ${BASH_TSTOUT}