terminal: pop signal handler when terminal is restored

When disable_bits() changes the terminal attributes it uses sigchain_push_common() to restore the terminal if a signal is received before restore_term() is called. However there is no corresponding call to sigchain_pop_common() when the settings are restored so the signal handler is left on the sigchain stack. This leaves the stack unbalanced so code such as sigchain_push_common(my_handler); ... read_key_without_echo(...); ... sigchain_pop_common(); pops the handler pushed by disable_bits() rather than the one it intended to. Additionally "git add -p" changes the terminal settings every time it reads a key press so the stack can grow significantly. In order to fix this save_term() now sets up the signal handler so restore_term() can unconditionally call sigchain_pop_common(). There are no callers of save_term() outside of terminal.c as the only external caller was removed by e3f7e01b50 ("Revert "editor: save and reset terminal after calling EDITOR"", 2021-11-22). Any future callers of save_term() should benefit from having the signal handler set up for them. Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Phillip Wood committed Feb 22, 2022 at 18:53 UTC f7da756566e8abf18abdf9e30a8ed6d2088770de
2 files changed +21 -4
compat/terminal.c
+13 -4
@@ -11,7 +11,7 @@
11 static void restore_term_on_signal(int sig)
12 {
13 restore_term();
14 - sigchain_pop(sig);
14 + /* restore_term calls sigchain_pop_common */
15 raise(sig);
16 }
17
@@ -31,14 +31,20 @@ void restore_term(void)
31 tcsetattr(term_fd, TCSAFLUSH, &old_term);
32 close(term_fd);
33 term_fd = -1;
34 + sigchain_pop_common();
35 }
36
37 int save_term(int full_duplex)
38 {
39 if (term_fd < 0)
40 term_fd = open("/dev/tty", O_RDWR);
41 + if (term_fd < 0)
42 + return -1;
43 + if (tcgetattr(term_fd, &old_term) < 0)
44 + return -1;
45 + sigchain_push_common(restore_term_on_signal);
46
41 - return (term_fd < 0) ? -1 : tcgetattr(term_fd, &old_term);
47 + return 0;
48 }
49
50 static int disable_bits(tcflag_t bits)
@@ -49,12 +55,12 @@ static int disable_bits(tcflag_t bits)
55 goto error;
56
57 t = old_term;
52 - sigchain_push_common(restore_term_on_signal);
58
59 t.c_lflag &= ~bits;
60 if (!tcsetattr(term_fd, TCSAFLUSH, &t))
61 return 0;
62
63 + sigchain_pop_common();
64 error:
65 close(term_fd);
66 term_fd = -1;
@@ -100,6 +106,8 @@ void restore_term(void)
106 return;
107 }
108
109 + sigchain_pop_common();
110 +
111 if (hconin == INVALID_HANDLE_VALUE)
112 return;
113
@@ -134,6 +142,7 @@ int save_term(int full_duplex)
142
143 GetConsoleMode(hconin, &cmode_in);
144 use_stty = 0;
145 + sigchain_push_common(restore_term_on_signal);
146 return 0;
147 error:
148 CloseHandle(hconin);
@@ -177,10 +186,10 @@ static int disable_bits(DWORD bits)
186 if (save_term(0) < 0)
187 return -1;
188
180 - sigchain_push_common(restore_term_on_signal);
189 if (!SetConsoleMode(hconin, cmode_in & ~bits)) {
190 CloseHandle(hconin);
191 hconin = INVALID_HANDLE_VALUE;
192 + sigchain_pop_common();
193 return -1;
194 }
195
compat/terminal.h
+8
@@ -1,7 +1,15 @@
1 #ifndef COMPAT_TERMINAL_H
2 #define COMPAT_TERMINAL_H
3
4 +/*
5 + * Save the terminal attributes so they can be restored later by a
6 + * call to restore_term(). Note that every successful call to
7 + * save_term() must be matched by a call to restore_term() even if the
8 + * attributes have not been changed. Returns 0 on success, -1 on
9 + * failure.
10 + */
11 int save_term(int full_duplex);
12 +/* Restore the terminal attributes that were saved with save_term() */
13 void restore_term(void);
14
15 char *git_terminal_prompt(const char *prompt, int echo);