Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions src/uu/sort/src/sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2042,13 +2042,23 @@ 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 => {

let f: OsString = {
#[cfg(unix)]
{
OsStr::from_bytes(&line).to_os_string()
}
#[cfg(not(unix))]
{
String::from_utf8_lossy(&line).into_owned().into()
}
};

match f.to_str() {
Some(s) if s == STDIN_FILE => {
return Err(SortError::MinusInStdIn.into());
}
"" => {
Some("") => {
return Err(SortError::ZeroLengthFileName {
file: files0_from,
line_num: line_num + 1,
Expand All @@ -2058,11 +2068,9 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
_ => {}
}

files.push(OsString::from(
std::str::from_utf8(&line)
.expect("Could not parse string from zero terminated input."),
));
files.push(f);
}

if files.is_empty() {
return Err(SortError::EmptyInputFile { file: files0_from }.into());
}
Expand Down
19 changes: 19 additions & 0 deletions tests/by-util/test_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1851,6 +1851,25 @@
.stdout_only("a\na\n");
}

#[test]
// Test for GNU tests/sort/sort-files0-from.pl "non-utf8"
#[cfg(unix)]
fn test_files0_from_non_utf8() {
use std::os::unix::ffi::OsStringExt;
let (at, mut ucmd) = at_and_ucmd!();

// non-UTF-8 bytes (0xFF)
let filename = std::ffi::OsString::from_vec(b"a\xffb".into());

Check failure on line 1862 in tests/by-util/test_sort.rs

View workflow job for this annotation

GitHub Actions / Style/spelling (ubuntu-latest, feat_os_unix)

ERROR: `cspell`: Unknown word 'xffb' (file:'tests/by-util/test_sort.rs', line:1862)
std::fs::write(at.plus(&filename), b"20\n10\n").unwrap();

let list_contents = vec![b'a', 0xFF, b'b', 0];
at.write_bytes("list0", &list_contents);

ucmd.args(&["--files0-from", "list0"])
.succeeds()
.stdout_only("10\n20\n");
}

#[test]
// Test for GNU tests/sort/sort-files0-from.pl "zero-len"
fn test_files0_from_zero_length() {
Expand Down
Loading