diff options
| author | Pádraig Brady <P@draigBrady.com> | 2025-06-22 16:40:04 +0100 |
|---|---|---|
| committer | Pádraig Brady <P@draigBrady.com> | 2025-06-22 17:00:02 +0100 |
| commit | 3d35b3c0e56bd556c90dc98c3e5e2e7289b0eb0d (patch) | |
| tree | 88d9df92341b4c1022d53d27fa97a704d725e7b0 | |
| parent | doc: stty: adjust description of supported speeds (diff) | |
| download | coreutils-3d35b3c0e56bd556c90dc98c3e5e2e7289b0eb0d.tar.gz coreutils-3d35b3c0e56bd556c90dc98c3e5e2e7289b0eb0d.zip | |
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.
| -rw-r--r-- | src/stty.c | 27 | ||||
| -rwxr-xr-x | tests/stty/stty-invalid.sh | 3 |
2 files changed, 15 insertions, 15 deletions
diff --git a/src/stty.c b/src/stty.c index 561de1c1a..0163ea48a 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 a1442a82d..868ed1d16 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 |
