From 3535bfdc7257d734d83f843c2dcac0a76a37fbb4 Mon Sep 17 00:00:00 2001 From: David Rebbe <1187684+ic3man5@users.noreply.github.com> Date: Mon, 20 Jan 2025 21:13:43 -0500 Subject: [PATCH 1/2] test(sort): add test for overflowing -k argument --- tests/by-util/test_sort.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index 62aa07dae5d..b6ce74cc85a 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -1302,3 +1302,14 @@ fn test_same_sort_mode_twice() { fn test_args_override() { new_ucmd!().args(&["-f", "-f"]).pipe_in("foo").succeeds(); } + +#[test] +fn test_k_overflow() { + let input = "2\n1\n"; + let output = "1\n2\n"; + new_ucmd!() + .args(&["-k", "18446744073709551616"]) + .pipe_in(input) + .succeeds() + .stdout_is(output); +} From 2ebdc4984dd463cf050cc1da3ecd07a7fc34b2b7 Mon Sep 17 00:00:00 2001 From: David Rebbe <1187684+ic3man5@users.noreply.github.com> Date: Mon, 20 Jan 2025 20:35:02 -0500 Subject: [PATCH 2/2] sort: set -k arg to usize::MAX on overflow newline, format, and more rust idiomatic code. refactor to remove panic! --- src/uu/sort/src/sort.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index 0c39c24421b..e6191410053 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -34,6 +34,7 @@ use std::ffi::{OsStr, OsString}; use std::fs::{File, OpenOptions}; use std::hash::{Hash, Hasher}; use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write}; +use std::num::IntErrorKind; use std::ops::Range; use std::path::Path; use std::path::PathBuf; @@ -696,9 +697,17 @@ impl KeyPosition { .ok_or_else(|| format!("invalid key {}", key.quote()))?; let char = field_and_char.next(); - let field = field - .parse() - .map_err(|e| format!("failed to parse field index {}: {}", field.quote(), e))?; + let field = match field.parse::() { + Ok(f) => f, + Err(e) if *e.kind() == IntErrorKind::PosOverflow => usize::MAX, + Err(e) => { + return Err(format!( + "failed to parse field index {} {}", + field.quote(), + e + )) + } + }; if field == 0 { return Err("field index can not be 0".to_string()); }