This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx-apps.git


The following commit(s) were added to refs/heads/master by this push:
     new 96f6e7d46 system/readline: restore terminal attributes safely
96f6e7d46 is described below

commit 96f6e7d46097ec01e6effbafd0a5f3594a54d32c
Author: raiden00pl <[email protected]>
AuthorDate: Sat Aug 22 07:42:22 2026 +0200

    system/readline: restore terminal attributes safely
    
    Track successful mode changes so failed setup cannot corrupt terminal state.
    
    Signed-off-by: raiden00pl <[email protected]>
    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 <unistd.h>
 #include <errno.h>
 #include <assert.h>
+#include <stdbool.h>
 #include <termios.h>
 
 #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);
     }

Reply via email to