Skip to content

Commit f731f2c

Browse files
authored
Merge pull request #7188 from ic3man5/main
sort: errors on overflowing -k argument but shouldn't
2 parents 9f7d174 + 2ebdc49 commit f731f2c

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/uu/sort/src/sort.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use std::ffi::{OsStr, OsString};
3434
use std::fs::{File, OpenOptions};
3535
use std::hash::{Hash, Hasher};
3636
use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write};
37+
use std::num::IntErrorKind;
3738
use std::ops::Range;
3839
use std::path::Path;
3940
use std::path::PathBuf;
@@ -696,9 +697,17 @@ impl KeyPosition {
696697
.ok_or_else(|| format!("invalid key {}", key.quote()))?;
697698
let char = field_and_char.next();
698699

699-
let field = field
700-
.parse()
701-
.map_err(|e| format!("failed to parse field index {}: {}", field.quote(), e))?;
700+
let field = match field.parse::<usize>() {
701+
Ok(f) => f,
702+
Err(e) if *e.kind() == IntErrorKind::PosOverflow => usize::MAX,
703+
Err(e) => {
704+
return Err(format!(
705+
"failed to parse field index {} {}",
706+
field.quote(),
707+
e
708+
))
709+
}
710+
};
702711
if field == 0 {
703712
return Err("field index can not be 0".to_string());
704713
}

tests/by-util/test_sort.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,3 +1302,14 @@ fn test_same_sort_mode_twice() {
13021302
fn test_args_override() {
13031303
new_ucmd!().args(&["-f", "-f"]).pipe_in("foo").succeeds();
13041304
}
1305+
1306+
#[test]
1307+
fn test_k_overflow() {
1308+
let input = "2\n1\n";
1309+
let output = "1\n2\n";
1310+
new_ucmd!()
1311+
.args(&["-k", "18446744073709551616"])
1312+
.pipe_in(input)
1313+
.succeeds()
1314+
.stdout_is(output);
1315+
}

0 commit comments

Comments
 (0)