Skip to content

Commit

Permalink
Merge pull request #7188 from ic3man5/main
Browse files Browse the repository at this point in the history
sort: errors on overflowing -k argument but shouldn't
  • Loading branch information
sylvestre authored Jan 22, 2025
2 parents 9f7d174 + 2ebdc49 commit f731f2c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/uu/sort/src/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<usize>() {
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());
}
Expand Down
11 changes: 11 additions & 0 deletions tests/by-util/test_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit f731f2c

Please sign in to comment.