uutils / uutils/coreutils

du panic (`into_string().unwrap`) on a non-UTF-8 filename in the dereferencing traversal

Open Beginner friendly
#12,769 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

U - du
Dominant language
Rust
Stars
24.1k
Forks
2k
Avg merge
1d 5h
Merged PRs (30d)
365

Description

Summary

When du walks a directory via its regular (non-safe) traversal — used whenever -L/--dereference is given, and always on non-Linux — it converts each entry name with entry.file_name().into_string().unwrap() to match it against --exclude patterns. A non-UTF-8 filename makes into_string() return Err, which is unwrapped and aborts the program (SIGABRT, exit 134). GNU du lists the directory normally and exits 0.

Steps to reproduce

$ mkdir -p duex/sub
$ python3 -c "import os; os.mkdir(b'duex/sub/\xff\xfedir')"   # non-UTF-8 dir name
$ du -L --exclude='zzz' duex
thread 'main' panicked at src/uu/du/src/du.rs:656:89:
called `Result::unwrap()` on an `Err` value: "\xFF\xFEdir"
$ echo $?
134

--exclude (any pattern) forces the name-matching path; -L selects the regular traversal on Linux. Plain du DIR on Linux uses the safe traversal and is unaffected.

Expected behavior

Match GNU: list the directory (the pattern simply doesn't match) and exit 0.

$ /usr/bin/du -L --exclude='zzz' duex
…
$ echo $?
0

Root cause

The exclude check unwraps the UTF-8 conversion of the entry name:

// src/uu/du/src/du.rs:656
… entry.file_name().into_string().unwrap() …

OsString::into_string() returns Err for any non-UTF-8 name. The sibling path check on the same line already uses to_string_lossy() for this_stat.path; the fix is to use to_string_lossy() here too (or match the pattern against the raw bytes) so a non-UTF-8 name is handled instead of aborting.

Found by our static analysis tooling.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the exclude check in src/uu/du/src/du.rs:656 and inspect how the dereferencing traversal converts entry names before matching patterns. Reproduce with the non-UTF-8 directory and du -L --exclude='zzz' duex; done means the directory is listed without a panic and the command exits 0.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
82/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.