Ondrej Oprala f6d304
			     BASH PATCH REPORT
Ondrej Oprala f6d304
			     =================
Ondrej Oprala f6d304
Ondrej Oprala f6d304
Bash-Release:	4.3
Ondrej Oprala f6d304
Patch-ID:	bash43-033
Ondrej Oprala f6d304
Ondrej Oprala f6d304
Bug-Reported-by:	mickael9@gmail.com, Jan Rome <jan.rome@gmail.com>
Ondrej Oprala f6d304
Bug-Reference-ID:	<20140907224046.382ED3610CC@mickael-laptop.localdomain>,
Ondrej Oprala f6d304
			<540D661D.50908@gmail.com>
Ondrej Oprala f6d304
Bug-Reference-URL:	http://lists.gnu.org/archive/html/bug-bash/2014-09/msg00029.html
Ondrej Oprala f6d304
			http://lists.gnu.org/archive/html/bug-bash/2014-09/msg00030.html
Ondrej Oprala f6d304
Ondrej Oprala f6d304
Bug-Description:
Ondrej Oprala f6d304
Ondrej Oprala f6d304
Bash does not clean up the terminal state in all cases where bash or
Ondrej Oprala f6d304
readline  modifies it and bash is subsequently terminated by a fatal signal.
Ondrej Oprala f6d304
This happens when the `read' builtin modifies the terminal settings, both
Ondrej Oprala f6d304
when readline is active and when it is not.  It occurs most often when a script
Ondrej Oprala f6d304
installs a trap that exits on a signal without re-sending the signal to itself.
Ondrej Oprala f6d304
Ondrej Oprala f6d304
Patch (apply with `patch -p0'):
Ondrej Oprala f6d304
Ondrej Oprala f6d304
*** ../bash-4.3-patched/shell.c	2014-01-14 08:04:32.000000000 -0500
Ondrej Oprala f6d304
--- shell.c	2014-12-22 10:27:50.000000000 -0500
Ondrej Oprala f6d304
***************
Ondrej Oprala f6d304
*** 74,77 ****
Ondrej Oprala f6d304
--- 74,78 ----
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
  #if defined (READLINE)
Ondrej Oprala f6d304
+ #  include <readline/readline.h>
Ondrej Oprala f6d304
  #  include "bashline.h"
Ondrej Oprala f6d304
  #endif
Ondrej Oprala f6d304
***************
Ondrej Oprala f6d304
*** 910,913 ****
Ondrej Oprala f6d304
--- 912,923 ----
Ondrej Oprala f6d304
    fflush (stderr);
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
+   /* Clean up the terminal if we are in a state where it's been modified. */
Ondrej Oprala f6d304
+ #if defined (READLINE)
Ondrej Oprala f6d304
+   if (RL_ISSTATE (RL_STATE_TERMPREPPED) && rl_deprep_term_function)
Ondrej Oprala f6d304
+     (*rl_deprep_term_function) ();
Ondrej Oprala f6d304
+ #endif
Ondrej Oprala f6d304
+   if (read_tty_modified ())
Ondrej Oprala f6d304
+     read_tty_cleanup ();
Ondrej Oprala f6d304
+ 
Ondrej Oprala f6d304
    /* Do trap[0] if defined.  Allow it to override the exit status
Ondrej Oprala f6d304
       passed to us. */
Ondrej Oprala f6d304
*** ../bash-4.3-patched/builtins/read.def	2014-10-01 12:57:38.000000000 -0400
Ondrej Oprala f6d304
--- builtins/read.def	2014-12-22 10:48:54.000000000 -0500
Ondrej Oprala f6d304
***************
Ondrej Oprala f6d304
*** 141,148 ****
Ondrej Oprala f6d304
  int sigalrm_seen;
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
! static int reading;
Ondrej Oprala f6d304
  static SigHandler *old_alrm;
Ondrej Oprala f6d304
  static unsigned char delim;
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
  /* In all cases, SIGALRM just sets a flag that we check periodically.  This
Ondrej Oprala f6d304
     avoids problems with the semi-tricky stuff we do with the xfree of
Ondrej Oprala f6d304
--- 141,150 ----
Ondrej Oprala f6d304
  int sigalrm_seen;
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
! static int reading, tty_modified;
Ondrej Oprala f6d304
  static SigHandler *old_alrm;
Ondrej Oprala f6d304
  static unsigned char delim;
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
+ static struct ttsave termsave;
Ondrej Oprala f6d304
+ 
Ondrej Oprala f6d304
  /* In all cases, SIGALRM just sets a flag that we check periodically.  This
Ondrej Oprala f6d304
     avoids problems with the semi-tricky stuff we do with the xfree of
Ondrej Oprala f6d304
***************
Ondrej Oprala f6d304
*** 189,193 ****
Ondrej Oprala f6d304
    SHELL_VAR *var;
Ondrej Oprala f6d304
    TTYSTRUCT ttattrs, ttset;
Ondrej Oprala f6d304
-   struct ttsave termsave;
Ondrej Oprala f6d304
  #if defined (ARRAY_VARS)
Ondrej Oprala f6d304
    WORD_LIST *alist;
Ondrej Oprala f6d304
--- 191,194 ----
Ondrej Oprala f6d304
***************
Ondrej Oprala f6d304
*** 222,226 ****
Ondrej Oprala f6d304
    USE_VAR(lastsig);
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
!   sigalrm_seen = reading = 0;
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
    i = 0;		/* Index into the string that we are reading. */
Ondrej Oprala f6d304
--- 223,227 ----
Ondrej Oprala f6d304
    USE_VAR(lastsig);
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
!   sigalrm_seen = reading = tty_modified = 0;
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
    i = 0;		/* Index into the string that we are reading. */
Ondrej Oprala f6d304
***************
Ondrej Oprala f6d304
*** 439,442 ****
Ondrej Oprala f6d304
--- 440,445 ----
Ondrej Oprala f6d304
  	  goto assign_vars;
Ondrej Oprala f6d304
  	}
Ondrej Oprala f6d304
+       if (interactive_shell == 0)
Ondrej Oprala f6d304
+ 	initialize_terminating_signals ();
Ondrej Oprala f6d304
        old_alrm = set_signal_handler (SIGALRM, sigalrm);
Ondrej Oprala f6d304
        add_unwind_protect (reset_alarm, (char *)NULL);
Ondrej Oprala f6d304
***************
Ondrej Oprala f6d304
*** 483,487 ****
Ondrej Oprala f6d304
--- 486,493 ----
Ondrej Oprala f6d304
  	  if (i < 0)
Ondrej Oprala f6d304
  	    sh_ttyerror (1);
Ondrej Oprala f6d304
+ 	  tty_modified = 1;
Ondrej Oprala f6d304
  	  add_unwind_protect ((Function *)ttyrestore, (char *)&termsave);
Ondrej Oprala f6d304
+ 	  if (interactive_shell == 0)
Ondrej Oprala f6d304
+ 	    initialize_terminating_signals ();
Ondrej Oprala f6d304
  	}
Ondrej Oprala f6d304
      }
Ondrej Oprala f6d304
***************
Ondrej Oprala f6d304
*** 498,502 ****
Ondrej Oprala f6d304
--- 504,511 ----
Ondrej Oprala f6d304
  	sh_ttyerror (1);
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
+       tty_modified = 1;
Ondrej Oprala f6d304
        add_unwind_protect ((Function *)ttyrestore, (char *)&termsave);
Ondrej Oprala f6d304
+       if (interactive_shell == 0)
Ondrej Oprala f6d304
+ 	initialize_terminating_signals ();
Ondrej Oprala f6d304
      }
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
***************
Ondrej Oprala f6d304
*** 589,592 ****
Ondrej Oprala f6d304
--- 598,603 ----
Ondrej Oprala f6d304
  	  else
Ondrej Oprala f6d304
  	    lastsig = 0;
Ondrej Oprala f6d304
+ 	  if (terminating_signal && tty_modified)
Ondrej Oprala f6d304
+ 	    ttyrestore (&termsave);	/* fix terminal before exiting */
Ondrej Oprala f6d304
  	  CHECK_TERMSIG;
Ondrej Oprala f6d304
  	  eof = 1;
Ondrej Oprala f6d304
***************
Ondrej Oprala f6d304
*** 979,982 ****
Ondrej Oprala f6d304
--- 990,1007 ----
Ondrej Oprala f6d304
  {
Ondrej Oprala f6d304
    ttsetattr (ttp->fd, ttp->attrs);
Ondrej Oprala f6d304
+   tty_modified = 0;
Ondrej Oprala f6d304
+ }
Ondrej Oprala f6d304
+ 
Ondrej Oprala f6d304
+ void
Ondrej Oprala f6d304
+ read_tty_cleanup ()
Ondrej Oprala f6d304
+ {
Ondrej Oprala f6d304
+   if (tty_modified)
Ondrej Oprala f6d304
+     ttyrestore (&termsave);
Ondrej Oprala f6d304
+ }
Ondrej Oprala f6d304
+ 
Ondrej Oprala f6d304
+ int
Ondrej Oprala f6d304
+ read_tty_modified ()
Ondrej Oprala f6d304
+ {
Ondrej Oprala f6d304
+   return (tty_modified);
Ondrej Oprala f6d304
  }
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
*** ../bash-4.3-patched/builtins/common.h	2014-10-01 12:57:47.000000000 -0400
Ondrej Oprala f6d304
--- builtins/common.h	2014-12-22 10:10:14.000000000 -0500
Ondrej Oprala f6d304
***************
Ondrej Oprala f6d304
*** 123,126 ****
Ondrej Oprala f6d304
--- 141,148 ----
Ondrej Oprala f6d304
  extern void getopts_reset __P((int));
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
+ /* Functions from read.def */
Ondrej Oprala f6d304
+ extern void read_tty_cleanup __P((void));
Ondrej Oprala f6d304
+ extern int read_tty_modified __P((void));
Ondrej Oprala f6d304
+ 
Ondrej Oprala f6d304
  /* Functions from set.def */
Ondrej Oprala f6d304
  extern int minus_o_option_value __P((char *));
Ondrej Oprala f6d304
*** ../bash-4.3-patched/bashline.c	2014-05-14 09:22:39.000000000 -0400
Ondrej Oprala f6d304
--- bashline.c	2014-09-08 11:28:56.000000000 -0400
Ondrej Oprala f6d304
***************
Ondrej Oprala f6d304
*** 203,206 ****
Ondrej Oprala f6d304
--- 203,207 ----
Ondrej Oprala f6d304
  extern int array_needs_making;
Ondrej Oprala f6d304
  extern int posixly_correct, no_symbolic_links;
Ondrej Oprala f6d304
+ extern int sigalrm_seen;
Ondrej Oprala f6d304
  extern char *current_prompt_string, *ps1_prompt;
Ondrej Oprala f6d304
  extern STRING_INT_ALIST word_token_alist[];
Ondrej Oprala f6d304
***************
Ondrej Oprala f6d304
*** 4209,4214 ****
Ondrej Oprala f6d304
    /* If we're going to longjmp to top_level, make sure we clean up readline.
Ondrej Oprala f6d304
       check_signals will call QUIT, which will eventually longjmp to top_level,
Ondrej Oprala f6d304
!      calling run_interrupt_trap along the way. */
Ondrej Oprala f6d304
!   if (interrupt_state)
Ondrej Oprala f6d304
      rl_cleanup_after_signal ();
Ondrej Oprala f6d304
    bashline_reset_event_hook ();
Ondrej Oprala f6d304
--- 4262,4268 ----
Ondrej Oprala f6d304
    /* If we're going to longjmp to top_level, make sure we clean up readline.
Ondrej Oprala f6d304
       check_signals will call QUIT, which will eventually longjmp to top_level,
Ondrej Oprala f6d304
!      calling run_interrupt_trap along the way.  The check for sigalrm_seen is
Ondrej Oprala f6d304
!      to clean up the read builtin's state. */
Ondrej Oprala f6d304
!   if (terminating_signal || interrupt_state || sigalrm_seen)
Ondrej Oprala f6d304
      rl_cleanup_after_signal ();
Ondrej Oprala f6d304
    bashline_reset_event_hook ();
Ondrej Oprala f6d304
*** ../bash-4.3-patched/sig.c	2014-01-10 15:06:06.000000000 -0500
Ondrej Oprala f6d304
--- sig.c	2014-09-08 11:26:33.000000000 -0400
Ondrej Oprala f6d304
***************
Ondrej Oprala f6d304
*** 533,538 ****
Ondrej Oprala f6d304
    /* Set the event hook so readline will call it after the signal handlers
Ondrej Oprala f6d304
       finish executing, so if this interrupted character input we can get
Ondrej Oprala f6d304
!      quick response. */
Ondrej Oprala f6d304
!   if (interactive_shell && interactive && no_line_editing == 0)
Ondrej Oprala f6d304
      bashline_set_event_hook ();
Ondrej Oprala f6d304
  #endif
Ondrej Oprala f6d304
--- 533,540 ----
Ondrej Oprala f6d304
    /* Set the event hook so readline will call it after the signal handlers
Ondrej Oprala f6d304
       finish executing, so if this interrupted character input we can get
Ondrej Oprala f6d304
!      quick response.  If readline is active or has modified the terminal we
Ondrej Oprala f6d304
!      need to set this no matter what the signal is, though the check for
Ondrej Oprala f6d304
!      RL_STATE_TERMPREPPED is possibly redundant. */
Ondrej Oprala f6d304
!   if (RL_ISSTATE (RL_STATE_SIGHANDLER) || RL_ISSTATE (RL_STATE_TERMPREPPED))
Ondrej Oprala f6d304
      bashline_set_event_hook ();
Ondrej Oprala f6d304
  #endif
Ondrej Oprala f6d304
*** ../bash-4.3/patchlevel.h	2012-12-29 10:47:57.000000000 -0500
Ondrej Oprala f6d304
--- patchlevel.h	2014-03-20 20:01:28.000000000 -0400
Ondrej Oprala f6d304
***************
Ondrej Oprala f6d304
*** 26,30 ****
Ondrej Oprala f6d304
     looks for to find the patch level (for the sccs version string). */
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
! #define PATCHLEVEL 32
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
  #endif /* _PATCHLEVEL_H_ */
Ondrej Oprala f6d304
--- 26,30 ----
Ondrej Oprala f6d304
     looks for to find the patch level (for the sccs version string). */
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
! #define PATCHLEVEL 33
Ondrej Oprala f6d304
  
Ondrej Oprala f6d304
  #endif /* _PATCHLEVEL_H_ */