From 2e84ad19480e6c302310eaea4e9009e59297902f Mon Sep 17 00:00:00 2001 From: weili <541602953@qq.com> Date: Thu, 11 Jun 2026 08:18:07 +0000 Subject: [PATCH] sort: do not panic on a non-UTF-8 --files0-from name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `sort --files0-from=F` decoded each NUL-separated filename with `str::from_utf8(&line).expect(...)`, aborting on any non-UTF-8 byte — Linux filenames are arbitrary byte strings. Carry the name as an `OsString` built from the raw bytes (via `uucore::os_string_from_vec`) and do the "-"/empty checks on the bytes, so a non-UTF-8 name flows into the normal open-failure path (exit 2) like GNU instead of aborting. --- src/uu/sort/src/sort.rs | 26 +++++++++----------------- tests/by-util/test_sort.rs | 10 ++++++++++ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/uu/sort/src/sort.rs b/src/uu/sort/src/sort.rs index d27f6eadb1a..d9af565c4f9 100644 --- a/src/uu/sort/src/sort.rs +++ b/src/uu/sort/src/sort.rs @@ -2042,26 +2042,18 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { path: files0_from.clone(), error, })?; - let f = std::str::from_utf8(&line) - .expect("Could not parse string from zero terminated input."); - match f { - STDIN_FILE => { - return Err(SortError::MinusInStdIn.into()); - } - "" => { - return Err(SortError::ZeroLengthFileName { - file: files0_from, - line_num: line_num + 1, - } - .into()); + if line.as_slice() == STDIN_FILE.as_bytes() { + return Err(SortError::MinusInStdIn.into()); + } + if line.is_empty() { + return Err(SortError::ZeroLengthFileName { + file: files0_from, + line_num: line_num + 1, } - _ => {} + .into()); } - files.push(OsString::from( - std::str::from_utf8(&line) - .expect("Could not parse string from zero terminated input."), - )); + files.push(uucore::os_string_from_vec(line)?); } if files.is_empty() { return Err(SortError::EmptyInputFile { file: files0_from }.into()); diff --git a/tests/by-util/test_sort.rs b/tests/by-util/test_sort.rs index 732b5c58b68..e32ef9bf018 100644 --- a/tests/by-util/test_sort.rs +++ b/tests/by-util/test_sort.rs @@ -1792,6 +1792,16 @@ fn test_files0_from_empty() { .stderr_only("sort: no input from 'file'\n"); } +#[test] +#[cfg(unix)] +fn test_files0_from_non_utf8_name() { + new_ucmd!() + .args(&["--files0-from", "-"]) + .pipe_in(vec![0xff_u8]) + .fails_with_code(2) + .stderr_contains("sort: cannot read"); +} + #[test] #[cfg(unix)] fn test_files0_read_error() {