On 25/11/2026 00:01, Collin Funk wrote:
This fixes a bug in the unreleased commit f8ba084b3 (stty: don't treat
baud rates of (speed_t) -1 as invalid, 2026-10-18).

* src/speedgen: Set valid to true in the returned speed_desc when the
speed is supported.
* tests/stty/stty.sh: Add a test case using the exta and extb speeds.
This got me looking at other speed parsing edge cases.
I'll apply the attached later to disallow:

  $ stty '.'
  $ stty 18446744073709551615.6

cheers,
Padraig
From 5a5f5e241224ce7ea8a84a662dfdd08066429694 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <[email protected]>
Date: Wed, 25 Nov 2026 13:42:19 +0000
Subject: [PATCH] stty: fix speed parsing edge cases

* src/stty.c (speed_to_baud): Remove ATTRIBUTE_PURE as strtoul()
could have set errno.  Handle overflow more robustly.
Also disallow a bare decimal point.
* tests/stty/stty-invalid.sh: Add test cases.
* NEWS: Augment the bug fix entry.
---
 NEWS                       |  5 +--
 src/stty.c                 | 66 +++++++++++++++-----------------------
 tests/stty/stty-invalid.sh |  7 ++--
 3 files changed, 34 insertions(+), 44 deletions(-)

diff --git a/NEWS b/NEWS
index e1e6bf4bb..dc24ae9b9 100644
--- a/NEWS
+++ b/NEWS
@@ -325,8 +325,9 @@ GNU coreutils NEWS                                    -*- outline -*-
   exits before it finishes reading all of its input.
   [bug introduced in coreutils-9.2]
 
-  'stty' again rejects empty speed arguments.
-  Previously such arguments were accepted as value 0.
+  'stty' again rejects empty speed arguments, a lone decimal point, and
+  speeds that overflow during parsing or rounding.
+  Previously such arguments could set the speed to 0.
   [bug introduced in coreutils-9.8]
 
   'stty' no longer treats the maximum value representable by the speed_t type as
diff --git a/src/stty.c b/src/stty.c
index 4fa378f7f..dd8663775 100644
--- a/src/stty.c
+++ b/src/stty.c
@@ -2322,13 +2322,10 @@ speed_matches (struct termios const *mode1, struct termios const *mode2)
   return true;
 }
 
-ATTRIBUTE_PURE
 static struct speed_desc
 string_to_baud (char const *arg)
 {
   char *ep;
-  unsigned long value;
-  unsigned char c;
 
   /* Explicitly disallow negative numbers.  */
   while (c_isspace (*arg))
@@ -2336,50 +2333,39 @@ string_to_baud (char const *arg)
   if (*arg == '-')
     goto invalid;
 
-  /* Empty does not mean 0.  */
-  if (! *arg)
+  errno = 0;
+  unsigned long value = strtoul (arg, &ep, 10);
+  if (errno == ERANGE)
     goto invalid;
-
-  value = strtoul (arg, &ep, 10);
-
-  c = *ep++;
-  if (c == '.')
+  if (ep == arg)
     {
-      /* Number includes a fraction. Round it to nearest-even.
-         Note in particular that 134.5 must round to 134! */
-      c = *ep++;
-      if (c)
-        {
-          unsigned char d = c - '0';
-          if (d > 5)
-            value++;
-          else if (d == 5)
-            {
-              while ((c = *ep++) == '0'); /* Skip zeroes after .5 */
-
-              if (c)
-                value++;                /* Nonzero, round up */
-              else
-                value += (value & 1);   /* Exactly in the middle, round even */
-            }
-
-          while (c_isdigit (c)) /* Skip remaining digits.  */
-            c = *ep++;
-
-          if (c)
-            goto invalid; /* Garbage after otherwise valid number */
-        }
-    }
-  else if (c)
-    {
-      /* Not a valid number; check for legacy aliases "exta" and "extb" */
       if (streq (arg, "exta"))
         return (struct speed_desc) { .baud = B19200, .valid = true };
-      else if (streq (arg, "extb"))
+      if (streq (arg, "extb"))
         return (struct speed_desc) { .baud = B38400, .valid = true };
-      else
+      if (*ep != '.' || !c_isdigit (ep[1]))
+        goto invalid;
+    }
+
+  if (*ep == '.')
+    {
+      /* Round to nearest, ties to even (e.g., 134.5 rounds to 134).  */
+      ep++;
+      bool round_up = '5' < *ep;
+      if (*ep == '5')
+        {
+          ep++;
+          while (*ep == '0')
+            ep++;
+          round_up = (value & 1) || c_isdigit (*ep);
+        }
+      while (c_isdigit (*ep))
+        ep++;
+      if (round_up && ckd_add (&value, value, 1))
         goto invalid;
     }
+  if (*ep)
+    goto invalid;
 
   return value_to_baud (value);
 
diff --git a/tests/stty/stty-invalid.sh b/tests/stty/stty-invalid.sh
index c85f0f125..5cbccbb80 100755
--- a/tests/stty/stty-invalid.sh
+++ b/tests/stty/stty-invalid.sh
@@ -54,10 +54,13 @@ fi
 # coreutils >= 9.8 supports arbitrary speeds on some systems
 # so restrict tests here to invalid numbers
 # We simulate unsupported numbers in a separate "LD_PRELOAD" test.
+# Note $ULONG_MAX is odd, so $ULONG_MAX.5 rounds to $ULONG_OFLOW.
 WRAP_9600="$(expr $ULONG_OFLOW - 9600)"
 for speed in 9599.. 9600.. 9600.5. 9600.50. 9600.0. ++9600 \
-             -$WRAP_9600 --$WRAP_9600 0x2580 96E2 9600,0 '9600.0 ' '' ' '; do
-  returns_ 1 stty ispeed "$speed" || fail=1
+             -$WRAP_9600 --$WRAP_9600 0x2580 96E2 9600,0 '9600.0 ' '' ' ' '.' \
+             "$ULONG_MAX.5" "$ULONG_MAX.6" "$ULONG_OFLOW" "$ULONG_OFLOW.6"; do
+  returns_ 1 stty ispeed "$speed" </dev/null 2>err || fail=1
+  grep 'invalid ispeed' err || fail=1
 done
 
 # Just in case either of the above mistakenly succeeds (and changes
-- 
2.56.0

Reply via email to