Fixes https://github.com/landley/toybox/issues/251 where `stty 300` was
mangling c_iflags to 0x300 because even if we don't match a full hex
specification of struct termios, sscanf() will have overwritten the
first value, which is c_iflag.
---
 toys/pending/stty.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
From ffa858352e10ad39c0130f07b3c6aead8fc4688d Mon Sep 17 00:00:00 2001
From: Elliott Hughes <e...@google.com>
Date: Wed, 14 Oct 2020 18:08:46 -0700
Subject: [PATCH] stty: don't mangle c_iflags.

Fixes https://github.com/landley/toybox/issues/251 where `stty 300` was
mangling c_iflags to 0x300 because even if we don't match a full hex
specification of struct termios, sscanf() will have overwritten the
first value, which is c_iflag.
---
 toys/pending/stty.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/toys/pending/stty.c b/toys/pending/stty.c
index 4168b9ba..a997f49d 100644
--- a/toys/pending/stty.c
+++ b/toys/pending/stty.c
@@ -330,7 +330,7 @@ void stty_main(void)
   xtcgetattr(&old);
 
   if (*toys.optargs) {
-    struct termios new = old;
+    struct termios new = old, tmp;
 
     for (i=0; toys.optargs[i]; i++) {
       char *arg = toys.optargs[i];
@@ -340,11 +340,15 @@ void stty_main(void)
       else if (!strcmp(arg, "line")) new.c_line = get_arg(&i, NR_LDISCS);
       else if (!strcmp(arg, "min")) new.c_cc[VMIN] = get_arg(&i, 255);
       else if (!strcmp(arg, "time")) new.c_cc[VTIME] = get_arg(&i, 255);
-      else if (sscanf(arg, "%x:%x:%x:%x:%n", &new.c_iflag, &new.c_oflag,
-                        &new.c_cflag, &new.c_lflag, &n) == 4)
+      else if (sscanf(arg, "%x:%x:%x:%x:%n", &tmp.c_iflag, &tmp.c_oflag,
+                        &tmp.c_cflag, &tmp.c_lflag, &n) == 4)
       {
         int value;
 
+        new.c_iflag = tmp.c_iflag;
+        new.c_oflag = tmp.c_oflag;
+        new.c_cflag = tmp.c_cflag;
+        new.c_lflag = tmp.c_lflag;
         arg += n;
         for (j=0;j<NCCS;j++) {
           if (sscanf(arg, "%x%n", &value, &n) != 1) error_exit("bad -g string");
-- 
2.29.0.rc1.297.gfa9743e501-goog

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to