Skip to content
/ git Public
forked from git/git

Commit f7da756

Browse files
phillipwoodgitster
authored andcommitted
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 e3f7e01 ("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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 24d7ce3 commit f7da756

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

compat/terminal.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
static void restore_term_on_signal(int sig)
1212
{
1313
restore_term();
14-
sigchain_pop(sig);
14+
/* restore_term calls sigchain_pop_common */
1515
raise(sig);
1616
}
1717

@@ -31,14 +31,20 @@ void restore_term(void)
3131
tcsetattr(term_fd, TCSAFLUSH, &old_term);
3232
close(term_fd);
3333
term_fd = -1;
34+
sigchain_pop_common();
3435
}
3536

3637
int save_term(int full_duplex)
3738
{
3839
if (term_fd < 0)
3940
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);
4046

41-
return (term_fd < 0) ? -1 : tcgetattr(term_fd, &old_term);
47+
return 0;
4248
}
4349

4450
static int disable_bits(tcflag_t bits)
@@ -49,12 +55,12 @@ static int disable_bits(tcflag_t bits)
4955
goto error;
5056

5157
t = old_term;
52-
sigchain_push_common(restore_term_on_signal);
5358

5459
t.c_lflag &= ~bits;
5560
if (!tcsetattr(term_fd, TCSAFLUSH, &t))
5661
return 0;
5762

63+
sigchain_pop_common();
5864
error:
5965
close(term_fd);
6066
term_fd = -1;
@@ -100,6 +106,8 @@ void restore_term(void)
100106
return;
101107
}
102108

109+
sigchain_pop_common();
110+
103111
if (hconin == INVALID_HANDLE_VALUE)
104112
return;
105113

@@ -134,6 +142,7 @@ int save_term(int full_duplex)
134142

135143
GetConsoleMode(hconin, &cmode_in);
136144
use_stty = 0;
145+
sigchain_push_common(restore_term_on_signal);
137146
return 0;
138147
error:
139148
CloseHandle(hconin);
@@ -177,10 +186,10 @@ static int disable_bits(DWORD bits)
177186
if (save_term(0) < 0)
178187
return -1;
179188

180-
sigchain_push_common(restore_term_on_signal);
181189
if (!SetConsoleMode(hconin, cmode_in & ~bits)) {
182190
CloseHandle(hconin);
183191
hconin = INVALID_HANDLE_VALUE;
192+
sigchain_pop_common();
184193
return -1;
185194
}
186195

compat/terminal.h

+8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
#ifndef COMPAT_TERMINAL_H
22
#define COMPAT_TERMINAL_H
33

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+
*/
411
int save_term(int full_duplex);
12+
/* Restore the terminal attributes that were saved with save_term() */
513
void restore_term(void);
614

715
char *git_terminal_prompt(const char *prompt, int echo);

0 commit comments

Comments
 (0)