Skip to content

xargs: treat whitespace-only input as no argument (fixes #771) - #819

Open
MsfPablo wants to merge 1 commit into
uutils:mainfrom
MsfPablo:fix/xargs-space-only-input-771
Open

xargs: treat whitespace-only input as no argument (fixes #771)#819
MsfPablo wants to merge 1 commit into
uutils:mainfrom
MsfPablo:fix/xargs-space-only-input-771

Conversation

@MsfPablo

@MsfPablo MsfPablo commented Aug 7, 2026

Copy link
Copy Markdown

Fixes #771.

Problem

When a filename (or any input) given to xargs consists solely of an ASCII space, uu xargs passes an empty-string argument to the child process:

$ echo -n " " > a.txt
$ ./target/debug/xargs -a a.txt ls
ls: cannot access ''

GNU xargs instead treats the lone space as a delimiter and runs the child with no extra arguments:

$ xargs -a a.txt ls
<lists the cwd, exit 0>

The same discrepancy occurs for any whitespace-only input (space, tab, newline, runs of them) on both stdin and -a.

Root cause

In src/xargs/mod.rs, the WhitespaceDelimitedArgumentReader::next loop returns Ok(None) at EOF only when no bytes have been read at all (i == 0). Input that was consumed but consisted purely of delimiters left result empty while i > 0, so the loop fell through and emitted a zero-length Argument. xargs then passed that empty string to the child.

Fix

At EOF, return Ok(None) whenever result is empty, not only when i == 0. This makes whitespace-only input behave exactly like empty input (delimiters separate arguments; a run of delimiters with no content between them yields no argument), matching GNU xargs. The i == 0 case is subsumed since no bytes read implies result is empty.

Behavior of normal tokenization (leading/trailing/multiple delimiters around real content, quoted/escaped args) is unchanged.

Test

Added xargs_whitespace_only_input covering space, tab, mixed whitespace, --no-run-if-empty, and the -a repro from the issue (asserting the child receives zero xargs-provided arguments). All existing xargs tests still pass.

cargo test          # 226 + 28 (xargs) tests, all pass
cargo fmt --check   # clean
cargo clippy --all-targets -- -D warnings   # clean

The acceptance criterion is GNU xargs behavior: whitespace-only input yields no argument.


Developed with AI assistance and reviewed by the contributor.

With input consisting solely of delimiters (e.g. a single ASCII space),
the whitespace-delimited argument reader would emit a zero-length token.
The child process then received an empty-string argument, producing
errors like `ls: cannot access ''`. GNU xargs treats a run of
delimiters with no content between them as producing no argument at all
(the command runs once with zero extra args, like empty input).

The reader's EOF handling returned `None` only when no bytes had been
read (`i == 0`). Input that was consumed but was pure whitespace left
`result` empty while `i > 0`, so the loop fell through and returned an
empty argument. Return `None` whenever `result` is empty at EOF, which
covers both the no-bytes and whitespace-only cases.
@codecov

codecov Bot commented Aug 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.93%. Comparing base (1f19cdd) to head (46778db).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #819   +/-   ##
=======================================
  Coverage   91.93%   91.93%           
=======================================
  Files          35       35           
  Lines        7251     7251           
  Branches      378      378           
=======================================
  Hits         6666     6666           
  Misses        443      443           
  Partials      142      142           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown

Commit 46778db has test result changes:

GNU findutils testsuite:

Test results comparison:
  Current:   TOTAL: 495 / PASSED: 418 / FAILED: 76 / SKIPPED: 1
  Reference: TOTAL: 495 / PASSED: 417 / FAILED: 77 / SKIPPED: 1

Changes from main branch:
  TOTAL: +0
  PASSED: +1
  FAILED: -1

Test improvements (1):
  + r

bfs testsuite:

Test results comparison:
  Current:   TOTAL: 314 / PASSED: 267 / FAILED: 41 / SKIPPED: 6
  Reference: TOTAL: 312 / PASSED: 266 / FAILED: 40 / SKIPPED: 6

Changes from main branch:
  TOTAL: +2
  PASSED: +1
  FAILED: +1

New test failures (1):
  - gnu/okdir_path_relative

@codspeed-hq

codspeed-hq Bot commented Aug 7, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing MsfPablo:fix/xargs-space-only-input-771 (46778db) with main (1f19cdd)

Open in CodSpeed

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.

gnucompability(xargs): when a filename contain an ascii space, it leads to an error

1 participant