Skip to content

sort: fix panic on non-UTF-8 filenames with --files0-from - #11593

Merged
sylvestre merged 33 commits into
uutils:mainfrom
ppmpreetham:fix/9692-Panics-on-Non-UTF-8
Jun 30, 2026
Merged

sylvestre merged 33 commits into
uutils:mainfrom
ppmpreetham:fix/9692-Panics-on-Non-UTF-8

Conversation

@ppmpreetham

Copy link
Copy Markdown
Contributor

Re-opening this to fix the hidden character warning in the previous branch name. Supersedes #11592.

Fixes #9696

Description

When parsing NUL-separated filenames via --files0-from, sort previously enforced strict UTF-8 validation using std::str::from_utf8().expect(...). This caused an immediate panic when encountering valid non-UTF-8 paths. GNU sort treats filenames as raw bytes and does not require them to be UTF-8 encoded.

This PR aligns uutils with GNU behavior by removing the strict UTF-8 enforcement:

  • On Unix: We now use OsStr::from_bytes to losslessly cast the raw bytes directly into an OsString, preserving arbitrary byte sequences exactly as GNU does.
  • On non-Unix (e.g., Windows): We safely fall back to String::from_utf8_lossy. This prevents the program from panicking or failing on invalid UTF-8 sequences, ensuring graceful error handling across all platforms.

Testing

Reproduced the issue and verified the fix locally (both linux and windows):

$ printf "20\n10\n" > "weird$(printf '\xff')name"
$ printf "weird$(printf '\xff')name\0" > list0
$ ./target/release/coreutils sort --files0-from=list0
10
20

@cakebaker cakebaker changed the title sort: fix panic on non-UTF-8 filenames with --files0-from ( Fixes #9696 )#11592 sort: fix panic on non-UTF-8 filenames with --files0-from Apr 2, 2026
@cakebaker

cakebaker commented Apr 2, 2026 •

Copy link
Copy Markdown
Contributor

Can you please add a test to tests/by_util/test_sort.rs to ensure we don't regress in the future? Thanks.

@github-actions

github-actions Bot commented Apr 2, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/tail/tail-n0f. tests/tail/tail-n0f is passing on 'main'. Maybe you have to rebase?
Skipping an intermittent issue tests/pr/bounded-memory (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tail/inotify-dir-recreate (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tty/tty-eof (passes in this run but fails in the 'main' branch)

@ppmpreetham

Copy link
Copy Markdown
Contributor Author

added a test for the same

@codspeed

codspeed Bot commented Apr 2, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 4.67%

❌ 1 regressed benchmark
✅ 326 untouched benchmarks
⏩ 46 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Simulation sort_ascii_c_locale 16 ms 16.7 ms -4.67%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing ppmpreetham:fix/9692-Panics-on-Non-UTF-8 (862042d) with main (b76d615)

Open in CodSpeed

Footnotes

  1. 46 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. ↩

@oech3

oech3 commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

please fix clippy

  error: unused import: `OsStringExt`
    --> src/uu/sort/src/sort.rs:42:36
     |
  42 | use std::os::unix::ffi::{OsStrExt, OsStringExt};
     |                                    ^^^^^^^^^^^
     |
     = note: `-D unused-imports` implied by `-D warnings`
     = help: to override `-D warnings` add `#[allow(unused_imports)]`

@github-actions

github-actions Bot commented Apr 2, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/pr/bounded-memory (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/tail/pipe-f is now passing!

Comment thread tests/by-util/test_sort.rs Outdated
let (at, mut ucmd) = at_and_ucmd!();

// non-UTF-8 bytes (0xFF)
let filename = std::ffi::OsString::from_vec(vec![b'a', 0xFF, b'b']);

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.

Suggested change
let filename = std::ffi::OsString::from_vec(vec![b'a', 0xFF, b'b']);
let filename = std::ffi::OsString::from_vec(b"a\xffb".into());

Something like this would be more readable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yes, but cspell stops me

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

just add it to the ignore list

Comment thread src/uu/sort/src/sort.rs Resolved
Comment thread src/uu/sort/src/sort.rs Outdated
Comment on lines +2047 to +2050
#[cfg(not(unix))]
let f_str = String::from_utf8_lossy(&line);
#[cfg(not(unix))]
let f = OsStr::new(f_str.as_ref());

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.

Suggested change
#[cfg(not(unix))]
let f_str = String::from_utf8_lossy(&line);
#[cfg(not(unix))]
let f = OsStr::new(f_str.as_ref());
#[cfg(not(unix))]
let f = {
let f_str = String::from_utf8_lossy(&line);
OsStr::new(f_str.as_ref())
};

Avoid writing #[cfg(not(unix))] more than once

Comment thread src/uu/sort/src/sort.rs Outdated
Comment on lines +2052 to +2060
if f == STDIN_FILE {
return Err(SortError::MinusInStdIn.into());
}
if f.is_empty() {
return Err(SortError::ZeroLengthFileName {
file: files0_from,
line_num: line_num + 1,
}
_ => {}
.into());

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.

Not sure why you can't keep the match here

@sylvestre

Copy link
Copy Markdown
Contributor

some jobs are failing btw :)

@github-actions

github-actions Bot commented Apr 5, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/numfmt/numfmt. tests/numfmt/numfmt is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/date/date-locale-hour (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/date/resolution (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/pr/bounded-memory (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/tail/tail-n0f is now being skipped but was previously passing.
Congrats! The gnu test tests/cp/copy-FMR is now passing!
Congrats! The gnu test tests/shuf/shuf-reservoir is now passing!
Congrats! The gnu test tests/sort/sort-stale-thread-mem is now passing!
Congrats! The gnu test tests/sort/sort-u-FMR is now passing!

@sylvestre

Copy link
Copy Markdown
Contributor

still fails

@github-actions

github-actions Bot commented Apr 6, 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)
Congrats! The gnu test tests/seq/seq-epipe is now passing!
Skip an intermittent issue tests/pr/bounded-memory (was skipped on 'main', now failing)

@ppmpreetham

ppmpreetham commented Apr 15, 2026 •

Copy link
Copy Markdown
Contributor Author

I'll look into this

Comment thread src/uucore/src/lib/lib.rs Outdated
Self::Byte(*value)
}
}
impl_from_for_enum!(CharByte:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is from the other pr

Comment thread Cargo.lock Outdated
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
"windows-sys 0.60.2",
"windows-sys 0.61.2",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i think it is unrelated to this pr

Comment thread .vscode/cSpell.json Outdated

// words to ignore (even if they are in the flagWords)
"ignoreWords": [],
"ignoreWords": ["xffb"],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please add it in .vscode/cspell.dictionaries/jargon.wordlist.txt instead

@ppmpreetham

Copy link
Copy Markdown
Contributor Author

It should work on all OSes now.
No issues currently.
Just cargo.lock needs to be rebuilt by CI/CD

@sylvestre

sylvestre commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

please remove your changes in cargo.lock
it should be done in a different pr

@ppmpreetham

Copy link
Copy Markdown
Contributor Author

done :)

@github-actions

github-actions Bot commented Jun 27, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/tail/tail-n0f (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/date/date-locale-hour (passes in this run but fails in the 'main' branch)
Congrats! The gnu test tests/printf/printf-surprise is now passing!

Comment thread tests/by-util/test_cut.rs Outdated
.args(args)
.ignore_stdin_write_error()
.pipe_in("a''b\n")
.ignore_stdin_write_error()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

seems unrelated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this was failing the tests..

in this test, cut -d "''" -f2 is invalid at argument parsing time. So cut can exit immediately, before reading a''b\n from stdin. The test writes .pipe_in(...) on a background thread...

if cut already exited, that write gets Broken pipe, and the test panics before the real assertion runs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

sorry but you touched sort ?! so, why it is impacting cut

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sort did not break cut. CI just reported another failing test in the same full run.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ok, remove it please
one pr = one change

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

okay, will remove it

Comment thread tests/by-util/test_sort.rs Outdated

#[test]
// Test for GNU tests/sort/sort-files0-from.pl "non-utf8"
#[cfg(unix)]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

seems unnecessary because of the next line

@sylvestre
sylvestre merged commit bb8f709 into uutils:main Jun 30, 2026
176 of 177 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

thanks

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

5 participants