Skip to content

xargs: do not panic on empty input in replace mode#736

Open
leeewee wants to merge 1 commit into
uutils:mainfrom
leeewee:xargs-fix-replace-empty-input
Open

xargs: do not panic on empty input in replace mode#736
leeewee wants to merge 1 commit into
uutils:mainfrom
leeewee:xargs-fix-replace-empty-input

Conversation

@leeewee

@leeewee leeewee commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Fixes #735

xargs -I {} (and -i, --replace) panicked on empty input:

$ printf '' | xargs -I {} echo hello {}
thread 'main' panicked at src/xargs/mod.rs:429:46:
index out of bounds: the len is 0 but the index is 0
$ echo $?
101

In replace mode xargs runs the command once per input item, so with empty input it should run nothing. Instead the final builder was still executed and CommandBuilder::execute indexed extra_args[0], which is empty when no items were read.

This treats replace mode as implying --no-run-if-empty (as GNU does), so empty input runs nothing and exits 0:

$ printf '' | xargs -I {} echo hello {} ; echo $?
0

Non-empty replace input still runs per item, and non-replace / -r behavior is unchanged. Added a regression test (xargs_replace_empty_input).

In replace mode (-I/-i/--replace) xargs runs the command once per input item, so
with empty input it should run nothing. Instead the final builder was still
executed and `CommandBuilder::execute` indexed `extra_args[0]`, which is empty
for no input, panicking with index-out-of-bounds. Treat replace mode as implying
--no-run-if-empty (as GNU does) so empty input runs nothing and exits 0.
@codecov

codecov Bot commented Jun 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.87%. Comparing base (795d29a) to head (02133dc).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #736      +/-   ##
==========================================
+ Coverage   91.86%   91.87%   +0.01%     
==========================================
  Files          35       35              
  Lines        7153     7152       -1     
  Branches      375      376       +1     
==========================================
  Hits         6571     6571              
  Misses        437      437              
+ Partials      145      144       -1     

☔ 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.

@codspeed-hq

codspeed-hq Bot commented Jun 12, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing leeewee:xargs-fix-replace-empty-input (02133dc) with main (795d29a)

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.

xargs panics (index out of bounds) with -I/-i/--replace on empty input

1 participant