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() {