util: Fix UB in SetStdinEcho when ENOTTY

This commit is contained in:
MarcoFalke committed 2026-02-18 21:28:46 +01:00
1 parent b65ff0e5a1
commit fa692974ac
2 files changed
+17 -10

No files matched your search

-6
View File
@@ -14,12 +14,6 @@
# Note that suppressions may depend on OS and/or library versions. # Note that suppressions may depend on OS and/or library versions.
# Tested on aarch64 and x86_64 with Ubuntu Noble system libs, using clang-16 # Tested on aarch64 and x86_64 with Ubuntu Noble system libs, using clang-16
# and GCC, without gui. # and GCC, without gui.
{
Suppress uninitialized bytes warning in compat code
Memcheck:Param
ioctl(TCSET{S,SW,SF})
fun:tcsetattr
}
{ {
Suppress leaks on shutdown Suppress leaks on shutdown
Memcheck:Leak Memcheck:Leak
+17 -4
View File
@@ -18,25 +18,38 @@
// https://stackoverflow.com/questions/1413445/reading-a-password-from-stdcin // https://stackoverflow.com/questions/1413445/reading-a-password-from-stdcin
void SetStdinEcho(bool enable) void SetStdinEcho(bool enable)
{ {
if (!StdinTerminal()) {
return;
}
#ifdef WIN32 #ifdef WIN32
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode; DWORD mode;
GetConsoleMode(hStdin, &mode); if (!GetConsoleMode(hStdin, &mode)) {
fputs("GetConsoleMode failed\n", stderr);
return;
}
if (!enable) { if (!enable) {
mode &= ~ENABLE_ECHO_INPUT; mode &= ~ENABLE_ECHO_INPUT;
} else { } else {
mode |= ENABLE_ECHO_INPUT; mode |= ENABLE_ECHO_INPUT;
} }
SetConsoleMode(hStdin, mode); if (!SetConsoleMode(hStdin, mode)) {
fputs("SetConsoleMode failed\n", stderr);
}
#else #else
struct termios tty; struct termios tty;
tcgetattr(STDIN_FILENO, &tty); if (tcgetattr(STDIN_FILENO, &tty) != 0) {
fputs("tcgetattr failed\n", stderr);
return;
}
if (!enable) { if (!enable) {
tty.c_lflag &= ~ECHO; tty.c_lflag &= ~ECHO;
} else { } else {
tty.c_lflag |= ECHO; tty.c_lflag |= ECHO;
} }
(void)tcsetattr(STDIN_FILENO, TCSANOW, &tty); if (tcsetattr(STDIN_FILENO, TCSANOW, &tty) != 0) {
fputs("tcsetattr failed\n", stderr);
}
#endif #endif
} }