133-upstream-bash43-033.patch 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. BASH PATCH REPORT
  2. =================
  3. Bash-Release: 4.3
  4. Patch-ID: bash43-033
  5. Bug-Reported-by: mickael9@gmail.com, Jan Rome <jan.rome@gmail.com>
  6. Bug-Reference-ID: <20140907224046.382ED3610CC@mickael-laptop.localdomain>,
  7. <540D661D.50908@gmail.com>
  8. Bug-Reference-URL: http://lists.gnu.org/archive/html/bug-bash/2014-09/msg00029.html
  9. http://lists.gnu.org/archive/html/bug-bash/2014-09/msg00030.html
  10. Bug-Description:
  11. Bash does not clean up the terminal state in all cases where bash or
  12. readline modifies it and bash is subsequently terminated by a fatal signal.
  13. This happens when the `read' builtin modifies the terminal settings, both
  14. when readline is active and when it is not. It occurs most often when a script
  15. installs a trap that exits on a signal without re-sending the signal to itself.
  16. Patch (apply with `patch -p0'):
  17. --- a/shell.c
  18. +++ b/shell.c
  19. @@ -73,6 +73,7 @@
  20. #endif
  21. #if defined (READLINE)
  22. +# include <readline/readline.h>
  23. # include "bashline.h"
  24. #endif
  25. @@ -909,6 +910,14 @@ exit_shell (s)
  26. fflush (stdout); /* XXX */
  27. fflush (stderr);
  28. + /* Clean up the terminal if we are in a state where it's been modified. */
  29. +#if defined (READLINE)
  30. + if (RL_ISSTATE (RL_STATE_TERMPREPPED) && rl_deprep_term_function)
  31. + (*rl_deprep_term_function) ();
  32. +#endif
  33. + if (read_tty_modified ())
  34. + read_tty_cleanup ();
  35. +
  36. /* Do trap[0] if defined. Allow it to override the exit status
  37. passed to us. */
  38. if (signal_is_trapped (0))
  39. --- a/builtins/read.def
  40. +++ b/builtins/read.def
  41. @@ -140,10 +140,12 @@ static void reset_alarm __P((void));
  42. procenv_t alrmbuf;
  43. int sigalrm_seen;
  44. -static int reading;
  45. +static int reading, tty_modified;
  46. static SigHandler *old_alrm;
  47. static unsigned char delim;
  48. +static struct ttsave termsave;
  49. +
  50. /* In all cases, SIGALRM just sets a flag that we check periodically. This
  51. avoids problems with the semi-tricky stuff we do with the xfree of
  52. input_string at the top of the unwind-protect list (see below). */
  53. @@ -188,7 +190,6 @@ read_builtin (list)
  54. struct stat tsb;
  55. SHELL_VAR *var;
  56. TTYSTRUCT ttattrs, ttset;
  57. - struct ttsave termsave;
  58. #if defined (ARRAY_VARS)
  59. WORD_LIST *alist;
  60. #endif
  61. @@ -221,7 +222,7 @@ read_builtin (list)
  62. USE_VAR(ps2);
  63. USE_VAR(lastsig);
  64. - sigalrm_seen = reading = 0;
  65. + sigalrm_seen = reading = tty_modified = 0;
  66. i = 0; /* Index into the string that we are reading. */
  67. raw = edit = 0; /* Not reading raw input by default. */
  68. @@ -438,6 +439,8 @@ read_builtin (list)
  69. retval = 128+SIGALRM;
  70. goto assign_vars;
  71. }
  72. + if (interactive_shell == 0)
  73. + initialize_terminating_signals ();
  74. old_alrm = set_signal_handler (SIGALRM, sigalrm);
  75. add_unwind_protect (reset_alarm, (char *)NULL);
  76. #if defined (READLINE)
  77. @@ -482,7 +485,10 @@ read_builtin (list)
  78. i = silent ? ttfd_cbreak (fd, &ttset) : ttfd_onechar (fd, &ttset);
  79. if (i < 0)
  80. sh_ttyerror (1);
  81. + tty_modified = 1;
  82. add_unwind_protect ((Function *)ttyrestore, (char *)&termsave);
  83. + if (interactive_shell == 0)
  84. + initialize_terminating_signals ();
  85. }
  86. }
  87. else if (silent) /* turn off echo but leave term in canonical mode */
  88. @@ -497,7 +503,10 @@ read_builtin (list)
  89. if (i < 0)
  90. sh_ttyerror (1);
  91. + tty_modified = 1;
  92. add_unwind_protect ((Function *)ttyrestore, (char *)&termsave);
  93. + if (interactive_shell == 0)
  94. + initialize_terminating_signals ();
  95. }
  96. /* This *must* be the top unwind-protect on the stack, so the manipulation
  97. @@ -588,6 +597,8 @@ read_builtin (list)
  98. }
  99. else
  100. lastsig = 0;
  101. + if (terminating_signal && tty_modified)
  102. + ttyrestore (&termsave); /* fix terminal before exiting */
  103. CHECK_TERMSIG;
  104. eof = 1;
  105. break;
  106. @@ -978,6 +989,20 @@ ttyrestore (ttp)
  107. struct ttsave *ttp;
  108. {
  109. ttsetattr (ttp->fd, ttp->attrs);
  110. + tty_modified = 0;
  111. +}
  112. +
  113. +void
  114. +read_tty_cleanup ()
  115. +{
  116. + if (tty_modified)
  117. + ttyrestore (&termsave);
  118. +}
  119. +
  120. +int
  121. +read_tty_modified ()
  122. +{
  123. + return (tty_modified);
  124. }
  125. #if defined (READLINE)
  126. --- a/builtins/common.h
  127. +++ b/builtins/common.h
  128. @@ -122,6 +122,10 @@ extern void bash_logout __P((void));
  129. /* Functions from getopts.def */
  130. extern void getopts_reset __P((int));
  131. +/* Functions from read.def */
  132. +extern void read_tty_cleanup __P((void));
  133. +extern int read_tty_modified __P((void));
  134. +
  135. /* Functions from set.def */
  136. extern int minus_o_option_value __P((char *));
  137. extern void list_minus_o_opts __P((int, int));
  138. --- a/bashline.c
  139. +++ b/bashline.c
  140. @@ -202,6 +202,7 @@ extern int current_command_line_count, s
  141. extern int last_command_exit_value;
  142. extern int array_needs_making;
  143. extern int posixly_correct, no_symbolic_links;
  144. +extern int sigalrm_seen;
  145. extern char *current_prompt_string, *ps1_prompt;
  146. extern STRING_INT_ALIST word_token_alist[];
  147. extern sh_builtin_func_t *last_shell_builtin, *this_shell_builtin;
  148. @@ -4208,8 +4209,9 @@ bash_event_hook ()
  149. {
  150. /* If we're going to longjmp to top_level, make sure we clean up readline.
  151. check_signals will call QUIT, which will eventually longjmp to top_level,
  152. - calling run_interrupt_trap along the way. */
  153. - if (interrupt_state)
  154. + calling run_interrupt_trap along the way. The check for sigalrm_seen is
  155. + to clean up the read builtin's state. */
  156. + if (terminating_signal || interrupt_state || sigalrm_seen)
  157. rl_cleanup_after_signal ();
  158. bashline_reset_event_hook ();
  159. check_signals_and_traps (); /* XXX */
  160. --- a/sig.c
  161. +++ b/sig.c
  162. @@ -532,8 +532,10 @@ termsig_sighandler (sig)
  163. #if defined (READLINE)
  164. /* Set the event hook so readline will call it after the signal handlers
  165. finish executing, so if this interrupted character input we can get
  166. - quick response. */
  167. - if (interactive_shell && interactive && no_line_editing == 0)
  168. + quick response. If readline is active or has modified the terminal we
  169. + need to set this no matter what the signal is, though the check for
  170. + RL_STATE_TERMPREPPED is possibly redundant. */
  171. + if (RL_ISSTATE (RL_STATE_SIGHANDLER) || RL_ISSTATE (RL_STATE_TERMPREPPED))
  172. bashline_set_event_hook ();
  173. #endif
  174. --- a/patchlevel.h
  175. +++ b/patchlevel.h
  176. @@ -25,6 +25,6 @@
  177. regexp `^#define[ ]*PATCHLEVEL', since that's what support/mkversion.sh
  178. looks for to find the patch level (for the sccs version string). */
  179. -#define PATCHLEVEL 32
  180. +#define PATCHLEVEL 33
  181. #endif /* _PATCHLEVEL_H_ */