From 96f6e7d46097ec01e6effbafd0a5f3594a54d32c Mon Sep 17 00:00:00 2001 From: raiden00pl Date: Sat, 22 Aug 2026 07:42:22 +0200 Subject: [PATCH] system/readline: restore terminal attributes safely Track successful mode changes so failed setup cannot corrupt terminal state. Signed-off-by: raiden00pl Assisted-by: Claude Code --- system/readline/readline_fd.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/system/readline/readline_fd.c b/system/readline/readline_fd.c index 39b7c7898..0490af266 100644 --- a/system/readline/readline_fd.c +++ b/system/readline/readline_fd.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include "system/readline.h" @@ -223,16 +224,19 @@ ssize_t readline_fd(FAR char *buf, int buflen, int infd, int outfd) struct readline_s vtbl; struct termios cfg; + struct termios newcfg; + bool restore_termios = false; ssize_t ret; - if (isatty(infd)) + if (isatty(infd) && tcgetattr(infd, &cfg) == 0 && + (cfg.c_lflag & ICANON) != 0) { - tcgetattr(infd, &cfg); - if (cfg.c_lflag & ICANON) + newcfg = cfg; + newcfg.c_lflag &= ~ICANON; + + if (tcsetattr(infd, TCSANOW, &newcfg) == 0) { - cfg.c_lflag &= ~ICANON; - tcsetattr(infd, TCSANOW, &cfg); - cfg.c_lflag |= ICANON; + restore_termios = true; } } @@ -251,7 +255,7 @@ ssize_t readline_fd(FAR char *buf, int buflen, int infd, int outfd) ret = readline_common(&vtbl.vtbl, buf, buflen); - if (isatty(infd) && (cfg.c_lflag & ICANON)) + if (restore_termios) { tcsetattr(infd, TCSANOW, &cfg); }