[PATCH 2/5] serial: core: clip out-of-range baud rate to min/max, not min+1/max-1

From: Hugo Villeneuve

Date: Tue Oct 20 2026 - 14:45:20 EST


From: Hugo Villeneuve <hvilleneuve@xxxxxxxxxxxx>

min and max values are valid baud rates, as the existing code in
uart_get_baud_rate() already checks:

if (baud >= min && baud <= max)
return baud;

I couldn't find any explanation in commit 16ae2a877bf4 ("serial: Fix crash
if the minimum rate of the device is > 9600 baud") as to why this is
clipped to min+1/max-1.

Also, Sashiko identified a potential problem where clipping to min+1/max-1
will be out of range when min == max. This would result in the loop exiting
and returning a zero baud rate, triggering a Division by zero fault.

Fix by clipping to min/max.

Fixes: 16ae2a877bf4 ("serial: Fix crash if the minimum rate of the device is > 9600 baud")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Hugo Villeneuve <hvilleneuve@xxxxxxxxxxxx>
---
drivers/tty/serial/serial_core.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_core.c
index c3c1a1d76405d..0498a8524c6da 100644
--- a/drivers/tty/serial/serial_core.c
+++ b/drivers/tty/serial/serial_core.c
@@ -533,10 +533,10 @@ uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
* As a last resort, if the range cannot be met then clip to
* the nearest chip supported rate.
*/
- if (baud <= min)
- baud = min + 1;
+ if (baud < min)
+ baud = min;
else
- baud = max - 1;
+ baud = max;

tty_termios_encode_baud_rate(termios, baud, baud);
}
--
2.47.3