ttinit: check retun value from tcgetattr(), only restore if OK

Found by Coverity Scan.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2021-04-15 10:11:06 +02:00
parent 8d3917e9fa
commit 6d380082eb
+11 -6
View File
@@ -370,16 +370,20 @@ int ttinit(void)
struct pollfd fd = { STDIN_FILENO, POLLIN, 0 };
struct winsize ws = { 0 };
struct termios tc, saved;
int cached = 0;
/*
* Basic TTY init, CLOCaL is important or TIOCWINSZ will block
* until DCD is asserted, and we won't ever get it.
*/
tcgetattr(STDERR_FILENO, &tc);
saved = tc;
tc.c_cflag |= (CLOCAL | CREAD);
tc.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
tcsetattr(STDERR_FILENO, TCSANOW, &tc);
if (!tcgetattr(STDERR_FILENO, &tc)) {
saved = tc;
cached = 1;
tc.c_cflag |= (CLOCAL | CREAD);
tc.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
tcsetattr(STDERR_FILENO, TCSANOW, &tc);
}
/* 1. Try TIOCWINSZ to query window size from kernel */
if (!ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws)) {
@@ -411,7 +415,8 @@ int ttinit(void)
fprintf(stderr, "\e8");
}
tcsetattr(STDERR_FILENO, TCSANOW, &saved);
if (cached)
tcsetattr(STDERR_FILENO, TCSANOW, &saved);
/* Sanity check */
if (ttcols <= 0)