From: Pádraig Brady Date: Sun, 22 Jun 2025 15:40:04 +0000 (+0100) Subject: stty: stricter floating point parsing X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d35b3c0e56bd556c90dc98c3e5e2e7289b0eb0d;p=thirdparty%2Fcoreutils.git stty: stricter floating point parsing * src/stty.c (string_to_baud): Disallow extraneous characters after floating point numbers. * tests/stty/stty-invalid.sh: Add test cases. --- diff --git a/src/stty.c b/src/stty.c index 561de1c1a0..0163ea48aa 100644 --- a/src/stty.c +++ b/src/stty.c @@ -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) diff --git a/tests/stty/stty-invalid.sh b/tests/stty/stty-invalid.sh index a1442a82d0..868ed1d16a 100755 --- a/tests/stty/stty-invalid.sh +++ b/tests/stty/stty-invalid.sh @@ -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