From 0be84b48e1d99256644bca845b384b8b4ea9a053 Mon Sep 17 00:00:00 2001 From: Sai Asish Y Date: Thu, 18 Jun 2026 12:39:45 -0700 Subject: [PATCH] more: avoid overflow when -n is the maximum u16 value Signed-off-by: Sai Asish Y --- src/uu/more/src/more.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/uu/more/src/more.rs b/src/uu/more/src/more.rs index 32c416a2073..63794933c96 100644 --- a/src/uu/more/src/more.rs +++ b/src/uu/more/src/more.rs @@ -123,7 +123,7 @@ impl Options { ) { // We add 1 to the number of lines to display because the last line // is used for the banner - (Some(n), _) | (None, Some(n)) if n > 0 => Some(n + 1), + (Some(n), _) | (None, Some(n)) if n > 0 => Some(n.saturating_add(1)), _ => None, // Use terminal height }; let from_line = match matches.get_one::(options::FROM_LINE).copied() { @@ -1137,6 +1137,14 @@ mod tests { assert!(!stdout.contains("2\n")); } + #[test] + fn test_lines_option_max_u16() { + // The +1 for the banner line used to overflow when -n is u16::MAX. + let matches = uu_app().get_matches_from(vec!["more", "-n", "65535"]); + let options = Options::from(&matches); + assert_eq!(options.lines, Some(u16::MAX)); + } + #[test] fn test_from_line_option() { let content = (0..5).map(|i| i.to_string() + "\n").collect::();