Skip to content

sort: --files0-from should not panic when invalid utf-8 - #9692

Closed
sylvestre wants to merge 1 commit into
uutils:mainfrom
sylvestre:sort-utf
Closed

sylvestre wants to merge 1 commit into
uutils:mainfrom
sylvestre:sort-utf

Conversation

@sylvestre

Copy link
Copy Markdown
Contributor

No description provided.

@sylvestre
sylvestre force-pushed the sort-utf branch 2 times, most recently from 140a726 to e1837a0 Compare December 18, 2025 09:57
@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/tail/overlay-headers (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/tail/inotify-dir-recreate is now passing!

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/tail/overlay-headers (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/tail/inotify-dir-recreate is now passing!

Comment thread tests/by-util/test_sort.rs Resolved
@sylvestre
sylvestre force-pushed the sort-utf branch 2 times, most recently from 57f5452 to 1c0f20e Compare December 18, 2025 17:23
@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Note: The gnu test tests/tail/inotify-dir-recreate is now being skipped but was previously passing.

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/misc/nohup is no longer failing!
Note: The gnu test tests/tail/inotify-dir-recreate is now being skipped but was previously passing.

Comment thread tests/by-util/test_sort.rs Resolved
Comment thread tests/by-util/test_sort.rs Resolved
@github-actions

github-actions Bot commented Jan 7, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/tty/tty-eof (passes in this run but fails in the 'main' branch)

@codspeed

codspeed Bot commented Jan 7, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by ×4.4

⚡ 2 improved benchmarks
✅ 63 untouched benchmarks
⏩ 280 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
⚡ Simulation true_consecutive_calls 28.3 µs 6.4 µs ×4.4
⚡ Simulation true_no_args 2,158.3 ns 842.5 ns ×2.6

Comparing sylvestre:sort-utf (910ed79) with main (e6cd47e)

Open in CodSpeed

Footnotes

  1. 280 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Comment thread src/uu/sort/src/sort.rs
Comment on lines +1313 to +1340
// Handle filenames as raw bytes to support non-UTF-8 paths
#[cfg(unix)]
let filename = {
use std::os::unix::ffi::OsStrExt;
OsStr::from_bytes(&line).to_owned()
};
#[cfg(not(unix))]
let filename = {
// On non-Unix systems, convert to UTF-8 with replacement chars
match std::str::from_utf8(&line) {
Ok(s) => OsString::from(s),
Err(_) => OsString::from(String::from_utf8_lossy(&line).into_owned()),
}
"" => {
return Err(SortError::ZeroLengthFileName {
file: files0_from,
line_num: line_num + 1,
}
.into());
};

// Check for special cases using bytes comparison
if line == b"-" {
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(filename);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The platform-specific code can be simplified using the existing uucore::os_string_from_vec helper, I was testing this locally. The only thing that would be functionally different is that it would error instead of doing lossy string conversion on non-unix which is what is done in a bunch of other utilities

Suggested change
// Handle filenames as raw bytes to support non-UTF-8 paths
#[cfg(unix)]
let filename = {
use std::os::unix::ffi::OsStrExt;
OsStr::from_bytes(&line).to_owned()
};
#[cfg(not(unix))]
let filename = {
// On non-Unix systems, convert to UTF-8 with replacement chars
match std::str::from_utf8(&line) {
Ok(s) => OsString::from(s),
Err(_) => OsString::from(String::from_utf8_lossy(&line).into_owned()),
}
"" => {
return Err(SortError::ZeroLengthFileName {
file: files0_from,
line_num: line_num + 1,
}
.into());
};
// Check for special cases using bytes comparison
if line == b"-" {
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(filename);
for (line_num, line) in buf_reader.split(b'\0').flatten().enumerate() {
// Check for special cases using bytes comparison
if line == b"-" {
return Err(SortError::MinusInStdIn.into());
}
if line.is_empty() {
return Err(SortError::ZeroLengthFileName {
file: files0_from,
line_num: line_num + 1,
}
.into());
}
let filename = uucore::os_string_from_vec(line)?;
files.push(filename);
}

Comment thread src/uu/sort/src/sort.rs
Comment on lines 1312 to 1341
for (line_num, line) in buf_reader.split(b'\0').flatten().enumerate() {
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());
// Handle filenames as raw bytes to support non-UTF-8 paths
#[cfg(unix)]
let filename = {
use std::os::unix::ffi::OsStrExt;
OsStr::from_bytes(&line).to_owned()
};
#[cfg(not(unix))]
let filename = {
// On non-Unix systems, convert to UTF-8 with replacement chars
match std::str::from_utf8(&line) {
Ok(s) => OsString::from(s),
Err(_) => OsString::from(String::from_utf8_lossy(&line).into_owned()),
}
"" => {
return Err(SortError::ZeroLengthFileName {
file: files0_from,
line_num: line_num + 1,
}
.into());
};

// Check for special cases using bytes comparison
if line == b"-" {
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(filename);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The platform-specific code can be simplified using the existing uucore::os_string_from_vec helper, I was testing this locally. The only thing that would be functionally different is that it would error instead of doing lossy string conversion on non-unix which is what is done in a bunch of other utilities

Suggested change
for (line_num, line) in buf_reader.split(b'\0').flatten().enumerate() {
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());
// Handle filenames as raw bytes to support non-UTF-8 paths
#[cfg(unix)]
let filename = {
use std::os::unix::ffi::OsStrExt;
OsStr::from_bytes(&line).to_owned()
};
#[cfg(not(unix))]
let filename = {
// On non-Unix systems, convert to UTF-8 with replacement chars
match std::str::from_utf8(&line) {
Ok(s) => OsString::from(s),
Err(_) => OsString::from(String::from_utf8_lossy(&line).into_owned()),
}
"" => {
return Err(SortError::ZeroLengthFileName {
file: files0_from,
line_num: line_num + 1,
}
.into());
};
// Check for special cases using bytes comparison
if line == b"-" {
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(filename);
}
for (line_num, line) in buf_reader.split(b'\0').flatten().enumerate() {
// Check for special cases using bytes comparison
if line == b"-" {
return Err(SortError::MinusInStdIn.into());
}
if line.is_empty() {
return Err(SortError::ZeroLengthFileName {
file: files0_from,
line_num: line_num + 1,
}
.into());
}
let filename = uucore::os_string_from_vec(line)?;
files.push(filename);
}

@github-actions

Copy link
Copy Markdown

GNU testsuite comparison:

Congrats! The gnu test tests/pr/bounded-memory is no longer failing!

Comment thread tests/by-util/test_sort.rs Resolved
Comment thread tests/by-util/test_sort.rs Resolved
ppmpreetham added a commit to ppmpreetham/coreutils that referenced this pull request Apr 2, 2026
ppmpreetham added a commit to ppmpreetham/coreutils that referenced this pull request Apr 6, 2026
@sylvestre

Copy link
Copy Markdown
Contributor Author

stuck, closing

@sylvestre sylvestre closed this Jun 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sort --files0-from Requires UTF-8 / Panics on Non‑UTF‑8 Filenames

3 participants