From 41e2b867bee3ef7b898ad7072d58f2ce44d35e8e Mon Sep 17 00:00:00 2001 From: Amr Gharbeia Date: Thu, 14 May 2026 14:10:58 -0400 Subject: [PATCH] fix: use O_RDONLY (0) for /dev/tty open, guard invalid fd --- src/backend/modern.lisp | 7 +++---- src/backend/simple.lisp | 5 ++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/backend/modern.lisp b/src/backend/modern.lisp index e63e360..348581b 100644 --- a/src/backend/modern.lisp +++ b/src/backend/modern.lisp @@ -173,11 +173,10 @@ as a fallback when a keyword is not in *named-colors*.") (values (sb-alien:deref winsize 1) ;; cols (sb-alien:deref winsize 0))) ;; rows (sb-alien:free-alien winsize)))) - ;; Direct ioctl on /dev/tty — opens the real controlling terminal, - ;; bypassing any fd weirdness from SBCL's subprocess or stream setup. + ;; Direct ioctl on /dev/tty — opens the real controlling terminal. (ignore-errors - (let ((tty-fd (sb-unix:unix-open "/dev/tty" 2 0))) ; O_RDWR = 2 - (when tty-fd + (let ((tty-fd (sb-unix:unix-open "/dev/tty" 0 0))) ; O_RDONLY + (when (and tty-fd (numberp tty-fd) (> tty-fd 0)) (unwind-protect (let* ((winsize (sb-alien:make-alien sb-alien:unsigned-short 4))) (sb-unix:unix-ioctl tty-fd 21523 (sb-alien:alien-sap winsize)) diff --git a/src/backend/simple.lisp b/src/backend/simple.lisp index 98f3e13..0e6dcd0 100644 --- a/src/backend/simple.lisp +++ b/src/backend/simple.lisp @@ -34,10 +34,9 @@ (values (sb-alien:deref winsize 1) (sb-alien:deref winsize 0))) (sb-alien:free-alien winsize)))) - ;; Direct ioctl on /dev/tty — opens the real controlling terminal. (ignore-errors - (let ((tty-fd (sb-unix:unix-open "/dev/tty" 2 0))) ; O_RDWR = 2 - (when tty-fd + (let ((tty-fd (sb-unix:unix-open "/dev/tty" 0 0))) ; O_RDONLY + (when (and tty-fd (numberp tty-fd) (> tty-fd 0)) (unwind-protect (let* ((winsize (sb-alien:make-alien sb-alien:unsigned-short 4))) (sb-unix:unix-ioctl tty-fd 21523 (sb-alien:alien-sap winsize))