xargs: treat whitespace-only input as no argument (fixes #771) - #819
Open
MsfPablo wants to merge 1 commit into
Open
xargs: treat whitespace-only input as no argument (fixes #771)#819MsfPablo wants to merge 1 commit into
MsfPablo wants to merge 1 commit into
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
Commit 46778db has test result changes: GNU findutils testsuite: bfs testsuite: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #771.
Problem
When a filename (or any input) given to
xargsconsists solely of an ASCII space,uu xargspasses an empty-string argument to the child process:GNU
xargsinstead treats the lone space as a delimiter and runs the child with no extra arguments: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, theWhitespaceDelimitedArgumentReader::nextloop returnsOk(None)at EOF only when no bytes have been read at all (i == 0). Input that was consumed but consisted purely of delimiters leftresultempty whilei > 0, so the loop fell through and emitted a zero-lengthArgument. xargs then passed that empty string to the child.Fix
At EOF, return
Ok(None)wheneverresultis empty, not only wheni == 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 GNUxargs. Thei == 0case is subsumed since no bytes read impliesresultis empty.Behavior of normal tokenization (leading/trailing/multiple delimiters around real content, quoted/escaped args) is unchanged.
Test
Added
xargs_whitespace_only_inputcovering space, tab, mixed whitespace,--no-run-if-empty, and the-arepro from the issue (asserting the child receives zero xargs-provided arguments). All existing xargs tests still pass.The acceptance criterion is GNU
xargsbehavior: whitespace-only input yields no argument.Developed with AI assistance and reviewed by the contributor.