1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

Merge pull request #7188 from ic3man5/main

sort: errors on overflowing -k argument but shouldn't
This commit is contained in:
Sylvestre Ledru 2025-01-22 10:05:51 +01:00 committed by GitHub
commit f731f2cba3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 3 deletions

View file

@ -34,6 +34,7 @@ use std::ffi::{OsStr, OsString};
use std::fs::{File, OpenOptions}; use std::fs::{File, OpenOptions};
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write}; use std::io::{stdin, stdout, BufRead, BufReader, BufWriter, Read, Write};
use std::num::IntErrorKind;
use std::ops::Range; use std::ops::Range;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
@ -696,9 +697,17 @@ impl KeyPosition {
.ok_or_else(|| format!("invalid key {}", key.quote()))?; .ok_or_else(|| format!("invalid key {}", key.quote()))?;
let char = field_and_char.next(); let char = field_and_char.next();
let field = field let field = match field.parse::<usize>() {
.parse() Ok(f) => f,
.map_err(|e| format!("failed to parse field index {}: {}", field.quote(), e))?; 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 { if field == 0 {
return Err("field index can not be 0".to_string()); return Err("field index can not be 0".to_string());
} }

View file

@ -1302,3 +1302,14 @@ fn test_same_sort_mode_twice() {
fn test_args_override() { fn test_args_override() {
new_ucmd!().args(&["-f", "-f"]).pipe_in("foo").succeeds(); 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);
}