Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Regression: 'read -s' does not disable echo



> On 03/01/2024 18:18 GMT Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
> So either "read -s" should ALWAYS read from SHTTY, or something more
> involved is needed here.

We could use ttyname to check that SHTTY and 0 are the same device.
Then we'd just document that -s and -d (which needs a further patch)
only work on the shell's own terminal.  The following seems to
do what I expect on Ubuntu.

The only simpler alternative I see is just ignoring fd 0 completely,
but that doesn't sound too safe.  I don't think terminal operations
need to be particularly efficient so unless there are OS oddities
with ttyname() this might be good enough.

pws

diff --git a/Src/builtin.c b/Src/builtin.c
index 9e08a1dbc..cf24f89c7 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -6506,8 +6506,16 @@ bin_read(char *name, char **args, Options ops, UNUSED(int func))
     } else
 	readfd = izle = 0;
 
-    if (OPT_ISSET(ops,'s') && SHTTY == readfd) {
+    if (OPT_ISSET(ops,'s') &&
+	(SHTTY == readfd || (readfd == 0 && isatty(0)))) {
 	struct ttyinfo ti;
+	char *tns = dupstring(ttyname(SHTTY));
+	char *tn0 = dupstring(ttyname(0));
+	if (!tns || !tn0 || strcmp(tns, tn0))
+	{
+	    zwarnnam(name, "Bad terminal for read -s: must be shell terminal");
+	    return 1;
+	}
 	memset(&ti, 0, sizeof(struct ttyinfo));
 	gettyinfo(&ti);
 	saveti = ti;
@@ -6555,7 +6563,7 @@ bin_read(char *name, char **args, Options ops, UNUSED(int func))
         delim = (unsigned char) ((delimstr[0] == Meta) ?
 			delimstr[1] ^ 32 : delimstr[0]);
 #endif
-	if (SHTTY == readfd) {
+	if (SHTTY == readfd || (readfd == 0 && isatty(0))) {
 	    struct ttyinfo ti;
 	    gettyinfo(&ti);
 	    if (! resettty) {




Messages sorted by: Reverse Date, Date, Thread, Author