Repository navigation
fix(tail): avoid preallocating from the requested line count - #319
Conversation
|
@strandly-the-agent please review |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. The one-line fix resolves #311, and the new regression test fails on main for the right reason. No findings. @ferdingler
- Repro:
echo a | tail -n 99999999999aborts atccae0f8(exit 134,memory allocation of 2400000000000 bytes failed) and printsawith exit 0 at2372871. - Regression test:
tail_large_line_counts_preserve_small_inputfails on base source with a panic atsrc/commands/tail.rs:64and passes at head. - Suite:
cargo test --workspace --all-targetsat head gave 2641 passed, 0 failed, 0 ignored (Linux aarch64, Rust 1.100.0, debug profile). - Not verified: doc tests,
--features nodetests, the Python and Node suites, clippy, and a release build. - CI at this SHA: Rust on ubuntu and windows is green. The macOS jobs were still queued or running when I looked, and
CI Gatewas red at that point.
What I ran
Head 2372871575717d3598d1a01954bbb5a5d283a063, base ccae0f8039fae3bc1d76a901e1a273952f5b89e2. Each tree was built into its own target directory, and the two binaries were run side by side.
| Command | Base | Head |
|---|---|---|
echo a | tail -n 99999999999 |
abort, exit 134 | a |
echo a | tail -99999999999 |
abort, exit 134 | a |
echo a | tail --lines=99999999999 |
abort, exit 134 | a |
tail -n 99999999999 /tmp/f |
abort, exit 134 | a |
echo a | tail -n 18446744073709551615 |
panic attempt to add with overflow |
a |
echo a | tail -n 9223372036854775807 |
panic capacity overflow |
a |
3000 lines through tail -n 99999999999 | wc -l |
abort, exit 134 | 3000 |
Behaviour that should not change is identical on both binaries: default count (last 10 of 25), -n 3, -n 0, -n +24, -n 25 and -n 26 on 25 lines, -n 2000 on 3000 lines (first line 1001, 2000 lines out), and -n 2999 on a 3000-line file.
Regression test on base. I checked out base and overlaid only the head tests/shell_integration.rs, then ran cargo test --test shell_integration tail_: 11 passed, 1 failed. The failure is tail_large_line_counts_preserve_small_input, with a panic at src/commands/tail.rs:64:66 followed by the stdout assertion at line 4502. At head the same command gives 12 passed. The usize::MAX / 2 case panics with capacity overflow rather than an arithmetic overflow, so the test should not depend on debug overflow checks; I only ran the debug profile.
Other checks. rustfmt --edition 2024 --check is clean on both changed files. src/commands/tail.rs:64 was the only place in tail.rs where the count sized an allocation; the +N path and the legacy -N form do not allocate from it.
Appendix: non-blocking, all true on main as well (3)
- Count above
usize::MAXfalls back to 10.src/commands/tail.rs:25usesval.parse().unwrap_or(10), so 25 lines throughtail -n 99999999999999999999999print 10 on both base and head. Outside #311 and not filed. - A panicking pipeline stage reports exit 0.
src/exec.rs:4097-4102leaves the stage's exit at 0 when its task fails. On base,echo a | tail -n 18446744073709551615panics and the shell exits 0. I found no command that triggers it at head, so not filed. - Unrelated to this PR:
echo $(( (-9223372036854775807-1) / -1 ))panics atsrc/exec.rs:1244(attempt to divide with overflow, exit 101) in a debug build at head. Not checked in release and not filed; I can open an issue if wanted.
Description
tail -n Nallocated a ring buffer fromNbefore reading any input. Very large counts could abort the host process, andusize::MAXoverflowed in debug builds. Start the ring buffer empty and let it grow with the input, retaining the existing last-N-lines behavior.Regression coverage checks huge counts on stdin, files and empty input, plus zero, one, exact-length and longer-than-input counts. The maximum-count test fails on unmodified main.
Related Issues
Fixes #311
Documentation PR
Not needed; this restores existing behavior.
Type of Change
Bug fix
Testing
cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyValidated on macOS against main after #315 with the full Rust check, workspace doctests, and Node-feature Rust unit tests. Both changes also passed direct CLI checks during initial validation. Existing lint warnings remain.
Checklist
Documentation changes are not needed: this restores existing shell behavior.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.