]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
stty: stricter floating point parsing
authorPádraig Brady <P@draigBrady.com>
Sun, 22 Jun 2025 15:40:04 +0000 (16:40 +0100)
committerPádraig Brady <P@draigBrady.com>
Sun, 22 Jun 2025 16:00:02 +0000 (17:00 +0100)
* src/stty.c (string_to_baud): Disallow extraneous characters
after floating point numbers.
* tests/stty/stty-invalid.sh: Add test cases.

src/stty.c
tests/stty/stty-invalid.sh

index 561de1c1a05ff4f4b6bda188d6c2ce5604a14e2b..0163ea48aa4a922149933a38558d8c2ae869cc2f 100644 (file)
@@ -2200,25 +2200,24 @@ string_to_baud (char const *arg)
       c = *ep++;
       if (c)
         {
-          c -= '0';
-          if (c > 9)
+          unsigned char d = c - '0';
+          if (d > 5)
+            value++;
+          else if (d == 5)
             {
-              return (speed_t) -1; /* Garbage after otherwise valid number */
-            }
-          else if (c > 5)
-            {
-              value++;
-            }
-          else if (c == 5)
-            {
-              while ((c = *ep++) == '0')
-              ; /* Skip zeroes after .5 */
+              while ((c = *ep++) == '0'); /* Skip zeroes after .5 */
 
-              if (c >= '1' && c <= '9')
-                value++;                /* Nonzero digit, round up */
+              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)
+            return (speed_t) -1; /* Garbage after otherwise valid number */
         }
     }
   else if (c)
index a1442a82d0d173ff5546376136b30eeb71a82048..868ed1d16a4c4193b5294e1db893e03d52813042 100755 (executable)
@@ -55,7 +55,8 @@ fi
 # so restrict tests here to invalid numbers
 # We simulate unsupported numbers in a separate "LD_PRELOAD" test.
 WRAP_9600="$(expr $ULONG_OFLOW - 9600)"
-for speed in 9600.. ++9600 -$WRAP_9600 --$WRAP_9600 0x2580 96E2; do
+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
 done