xargs: cap single-argument growth to avoid OOM on unterminated input - #820
Open
MsfPablo wants to merge 1 commit into
Open
xargs: cap single-argument growth to avoid OOM on unterminated input#820MsfPablo wants to merge 1 commit into
MsfPablo wants to merge 1 commit into
Conversation
Reading from an input that never produces a delimiter (such as `/dev/full`, which yields an endless stream of NUL bytes) made the argument readers accumulate a single argument without bound until the process was OOM-killed, because nothing bounded the growth between delimiters. Bound each accumulated argument by a multiple of the effective command-line character budget (the same budget the `-s` / system ARG_MAX limiters enforce), reporting `argument line too long` once an argument exceeds it. The cap is sized generously (4x the budget) so that no argument the size limiters would ever accept can be rejected by the reader. The byte-delimited reader previously relied on `BufReader::read_until` to loop internally; driving the buffered reader by hand (so the cap can be checked) surfaces `Interrupted` reads to the caller, so retry those explicitly to preserve the previous behaviour.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #820 +/- ##
==========================================
+ Coverage 91.93% 91.94% +0.01%
==========================================
Files 35 35
Lines 7251 7300 +49
Branches 378 383 +5
==========================================
+ Hits 6666 6712 +46
- Misses 443 445 +2
- Partials 142 143 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Commit 44f265b has test result changes: 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.
Closes #770.
Problem
xargsreading from an input that never produces a delimiter — most notably/dev/full, which yields an endless stream ofNULbytes — accumulates a single argument without bound until the process is OOM-killed. Nothing bounded argument growth between delimiters, so a terminator-less (or non-matching-delimiter) stream could grow the reader buffer indefinitely.A minimal repro (Linux, with
/dev/full):Fix
Bound each accumulated argument by a multiple of the effective command-line character budget — the same budget the
-s/ systemARG_MAX-derived limiters already enforce — and reportargument line too longonce an argument exceeds it. This mirrors the error GNUxargsproduces in this situation.MaxCharsCommandSizeLimiter::effective_max_chars(options_max_chars, env)returns the per-command-line budget (min of user-sand the system limit, or the 128 KiB default), used both to construct the existing limiter and to derive the argument cap.ARG_SIZE_OVERFLOW_MULTIPLIER = 4: the cap is generously sized so that no argument the size limiters would ever accept can be rejected by the reader — it only ever trips on truly unbounded growth.WhitespaceDelimitedArgumentReaderandByteDelimitedArgumentReadergain amax_arg_sizebound, checked as bytes are accumulated.io::Resultto a smallXargsErrorenum so theargument line too longcondition is distinguishable from an ordinary I/O error (e.g.Interrupted,BrokenPipe).The byte-delimited reader previously relied on
BufReader::read_untilto loop internally; driving the buffered reader by hand (so the cap can be checked) surfacesInterruptedreads to the caller, so those are retried explicitly to preserve the previous behaviour.Verification
cargo build— cleancargo clippy --all-targets -- -D warnings— cleancargo fmt --check— cleancargo test— 360 tests pass, 0 failuresAdded two unit tests covering the cap directly (portable, no
/dev/fulldependency) so the behaviour is locked in regardless of platform:test_whitespace_reader_caps_unbounded_argument— a long run of non-whitespace with no terminator returnsArgumentTooLargeinstead of looping forever.test_byte_reader_caps_unbounded_argument— a stream lacking the delimiter byte returnsArgumentTooLargeinstead of looping forever.Existing reader tests were updated to the new constructor signatures and the
XargsErrorreturn type; theInterrupted-retried andBrokenPipe-propagation behaviours are still covered bytest_byte_delimited_readerandtest_eof_argument_reader./dev/fullitself is Linux-only, so I kept the end-to-end repro on Linux rather than gating a test behindcfg(target_os = "linux"); the unit tests above exercise the same code path portably.