Skip to content

fix for #22443: prevent stty from being called when not running in a terminal #22444

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions dist/libexec/common-shared
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@
# * Credits: This script is based on the script generated by sbt-pack.
# *--------------------------------------------------------------------------*/

# save terminal settings
saved_stty=$(stty -g 2>/dev/null)
# clear on error so we don't later try to restore them
if [[ ! $? ]]; then
saved_stty=""
if [ -e /usr/bin/tty -a "`tty`" != "not a tty" -a ! -p /dev/stdin ]; then
Copy link
Member

@hamzaremmal hamzaremmal Mar 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked and played a bit with the locale and it seems that tty will always output 'not a tty' regardless of the current locale. Which means that we can rely on that output being in the same language.

% date                   
Sun Mar  9 14:58:45 CET 2025
% tty < /dev/null
not a tty
% export LANG=fr_FR.UTF-8
% date                   
Dim  9 mar 2025 14:59:00 CET
% tty < /dev/null        
not a tty

isterminal=1
# save terminal settings
saved_stty=$(stty -g 2>/dev/null)
# clear on error so we don't later try to restore them
if [[ ! $? ]]; then
saved_stty=""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you clear the cached stty but you don't clear the isterminal variable. The code below will try to recover the stty if isterminal is set to 1. Locally, recovering the stty with these parameters might be problematic:

% stty
speed 9600 baud;
lflags: echoe echoke echoctl pendin
iflags: iutf8
oflags: -oxtabs
cflags: cs8 -parenb
% stty ""
stty: illegal option -- 
usage: stty [-a | -e | -g] [-f file] [arguments]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point. Clearing isterminal when stty is cleared seems correct.

isterminal=0
fi
else
isterminal=0
fi

# restore stty settings (echo in particular)
function restoreSttySettings() {
stty $saved_stty
[ $isterminal -eq 1 ] && stty $saved_stty
saved_stty=""
}

Expand Down Expand Up @@ -61,7 +67,7 @@ if [[ ${cygwin-} || ${mingw-} || ${msys-} ]]; then
CYGPATHCMD=`which cygpath 2>/dev/null`
case "$TERM" in
rxvt* | xterm* | cygwin*)
stty -icanon min 1 -echo
[ $isterminal -eq 1 ] && stty -icanon min 1 -echo
JAVA_OPTS="$JAVA_OPTS -Djline.terminal=unix"
;;
esac
Expand Down
Loading