462fb2
			     BASH PATCH REPORT
462fb2
			     =================
462fb2
462fb2
Bash-Release:	4.2
462fb2
Patch-ID:	bash42-012
462fb2
462fb2
Bug-Reported-by:	Rui Santos <rsantos@grupopie.com>
462fb2
Bug-Reference-ID:	<4E04C6D0.2020507@grupopie.com>
462fb2
Bug-Reference-URL:	http://lists.gnu.org/archive/html/bug-bash/2011-06/msg00079.html
462fb2
462fb2
Bug-Description:
462fb2
462fb2
When calling the parser to recursively parse a command substitution within
462fb2
an arithmetic expansion, the shell overwrote the saved shell input line and
462fb2
associated state, resulting in a garbled command.
462fb2
462fb2
Patch (apply with `patch -p0'):
462fb2
462fb2
*** ../bash-4.2-patched/parse.y	2011-02-26 19:19:05.000000000 -0500
462fb2
--- parse.y	2011-06-24 20:08:22.000000000 -0400
462fb2
***************
462fb2
*** 3843,3846 ****
462fb2
--- 3849,3853 ----
462fb2
  {
462fb2
    sh_parser_state_t ps;
462fb2
+   sh_input_line_state_t ls;
462fb2
    int orig_ind, nc, sflags;
462fb2
    char *ret, *s, *ep, *ostring;
462fb2
***************
462fb2
*** 3850,3857 ****
462fb2
--- 3857,3866 ----
462fb2
    ostring = string;
462fb2
  
462fb2
+ /*itrace("xparse_dolparen: size = %d shell_input_line = `%s'", shell_input_line_size, shell_input_line);*/
462fb2
    sflags = SEVAL_NONINT|SEVAL_NOHIST|SEVAL_NOFREE;
462fb2
    if (flags & SX_NOLONGJMP)
462fb2
      sflags |= SEVAL_NOLONGJMP;
462fb2
    save_parser_state (&ps);
462fb2
+   save_input_line_state (&ls);
462fb2
  
462fb2
    /*(*/
462fb2
***************
462fb2
*** 3862,3865 ****
462fb2
--- 3871,3876 ----
462fb2
    restore_parser_state (&ps);
462fb2
    reset_parser ();
462fb2
+   /* reset_parser clears shell_input_line and associated variables */
462fb2
+   restore_input_line_state (&ls);
462fb2
    if (interactive)
462fb2
      token_to_read = 0;
462fb2
***************
462fb2
*** 5909,5912 ****
462fb2
--- 5920,5929 ----
462fb2
    ps->echo_input_at_read = echo_input_at_read;
462fb2
  
462fb2
+   ps->token = token;
462fb2
+   ps->token_buffer_size = token_buffer_size;
462fb2
+   /* Force reallocation on next call to read_token_word */
462fb2
+   token = 0;
462fb2
+   token_buffer_size = 0;
462fb2
+ 
462fb2
    return (ps);
462fb2
  }
462fb2
***************
462fb2
*** 5950,5953 ****
462fb2
--- 5967,6006 ----
462fb2
    expand_aliases = ps->expand_aliases;
462fb2
    echo_input_at_read = ps->echo_input_at_read;
462fb2
+ 
462fb2
+   FREE (token);
462fb2
+   token = ps->token;
462fb2
+   token_buffer_size = ps->token_buffer_size;
462fb2
+ }
462fb2
+ 
462fb2
+ sh_input_line_state_t *
462fb2
+ save_input_line_state (ls)
462fb2
+      sh_input_line_state_t *ls;
462fb2
+ {
462fb2
+   if (ls == 0)
462fb2
+     ls = (sh_input_line_state_t *)xmalloc (sizeof (sh_input_line_state_t));
462fb2
+   if (ls == 0)
462fb2
+     return ((sh_input_line_state_t *)NULL);
462fb2
+ 
462fb2
+   ls->input_line = shell_input_line;
462fb2
+   ls->input_line_size = shell_input_line_size;
462fb2
+   ls->input_line_len = shell_input_line_len;
462fb2
+   ls->input_line_index = shell_input_line_index;
462fb2
+ 
462fb2
+   /* force reallocation */
462fb2
+   shell_input_line = 0;
462fb2
+   shell_input_line_size = shell_input_line_len = shell_input_line_index = 0;
462fb2
+ }
462fb2
+ 
462fb2
+ void
462fb2
+ restore_input_line_state (ls)
462fb2
+      sh_input_line_state_t *ls;
462fb2
+ {
462fb2
+   FREE (shell_input_line);
462fb2
+   shell_input_line = ls->input_line;
462fb2
+   shell_input_line_size = ls->input_line_size;
462fb2
+   shell_input_line_len = ls->input_line_len;
462fb2
+   shell_input_line_index = ls->input_line_index;
462fb2
+ 
462fb2
+   set_line_mbstate ();
462fb2
  }
462fb2
  
462fb2
*** ../bash-4.2-patched/shell.h	2011-01-06 22:16:55.000000000 -0500
462fb2
--- shell.h	2011-06-24 19:12:25.000000000 -0400
462fb2
***************
462fb2
*** 137,140 ****
462fb2
--- 139,145 ----
462fb2
    int *token_state;
462fb2
  
462fb2
+   char *token;
462fb2
+   int token_buffer_size;
462fb2
+ 
462fb2
    /* input line state -- line number saved elsewhere */
462fb2
    int input_line_terminator;
462fb2
***************
462fb2
*** 167,171 ****
462fb2
--- 172,186 ----
462fb2
  } sh_parser_state_t;
462fb2
  
462fb2
+ typedef struct _sh_input_line_state_t {
462fb2
+   char *input_line;
462fb2
+   int input_line_index;
462fb2
+   int input_line_size;
462fb2
+   int input_line_len;
462fb2
+ } sh_input_line_state_t;
462fb2
+ 
462fb2
  /* Let's try declaring these here. */
462fb2
  extern sh_parser_state_t *save_parser_state __P((sh_parser_state_t *));
462fb2
  extern void restore_parser_state __P((sh_parser_state_t *));
462fb2
+ 
462fb2
+ extern sh_input_line_state_t *save_input_line_state __P((sh_input_line_state_t *));
462fb2
+ extern void restore_input_line_state __P((sh_input_line_state_t *));
462fb2
*** ../bash-4.2-patched/patchlevel.h	Sat Jun 12 20:14:48 2010
462fb2
--- patchlevel.h	Thu Feb 24 21:41:34 2011
462fb2
***************
462fb2
*** 26,30 ****
462fb2
     looks for to find the patch level (for the sccs version string). */
462fb2
  
462fb2
! #define PATCHLEVEL 11
462fb2
  
462fb2
  #endif /* _PATCHLEVEL_H_ */
462fb2
--- 26,30 ----
462fb2
     looks for to find the patch level (for the sccs version string). */
462fb2
  
462fb2
! #define PATCHLEVEL 12
462fb2
  
462fb2
  #endif /* _PATCHLEVEL_H_ */