Repository navigation
fix(tail): reject an unparseable -n count and accept a leading - - #349
Conversation
tail replaced a count it could not parse with a default, so tail -n -2 printed 10 lines and tail -n abc printed everything with status 0. Parse the count with head's signed_count, which accepts the optional sign and propagates the error. Fixes strands-agents#253
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Changes requested: the #253 fix itself is correct, but a later -n now cancels an earlier +N, which GNU tail and main don't do. It needs -n twice to hit, and the fix is one character (inline).
| Pass | Result |
|---|---|
| Correctness and tests | 🔴 1 inline. Both new cases fail on main and pass here. |
Adversarial (GNU 8.32 and 9.1 vs main vs PR) |
Same regression. head is unchanged, no panics or hangs. |
Local gates at c1774a4 |
cargo test --workspace --all-targets: 2733 passed, 0 failed. cargo fmt --check and cargo clippy exit 0. |
One non-blocking question: should a count with leading whitespace be accepted? GNU accepts it, and this shell's own wc -l < f produces one (details below).
What I verified, the question, and appendix
Verified
- Head
c1774a4fc2be88c2b8af7b4a590f2015a4323849against basedbb6189b6c6e4d124732fd228a89f2cef9309d9f. - The issue's two repros:
tail -n -2prints11 12, andtail -n abcexits 1 with a message on stderr. Onmainthey print 10 lines and the whole input with status 0. - Copying the two new cases onto
mainmakescargo test --test differentialfail; it passes on the PR. - Both
.expectedfiles match bash with GNU tail 8.32 and 9.1. head -nandhead -cbehave the same onmainand the PR across about 3,800 fuzzed commands, so makingsigned_countpub(super)changed nothing there.- About 7,600 single
-nvalues compared with GNU: the PR never accepts a value GNU rejects. The only values it rejects that GNU accepts are leading whitespace and multiplier suffixes. - Error path:
||,&&,set -e, pipefail,$(...)and subshells behave like GNU aftertail -n abc.
Question (non-blocking): leading whitespace in the count
n=$(wc -l < f); tail -n "$n" f now fails with cannot parse argument " 3", because this shell's wc pads a count read from stdin to 7 columns (the recorded known-difference in tests/differential/cases/wc_counts.sh). GNU tail skips leading whitespace and prints the lines. On main this printed the last 10 lines, which was only right for short files, so the error is still an improvement. head already rejects the same input on main. Skipping leading whitespace in signed_count would fix both commands; a follow-up is fine.
Appendix, non-blocking (2)
- GNU multiplier suffixes (
tail -n 1k) are now an error instead of a silent 10 lines. GNU accepts them;headalready rejects them. - The error text is
cannot parse argument "abc": invalid digit found in stringrather than GNU'sinvalid number of lines. It matcheshead, and the differential rules only compare whether stderr is empty.
Not covered: the Python and Node bindings and the wasm build were not run.
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. My one finding is fixed at 1dca4a7: a later -n keeps an earlier +N, as GNU tail does, and the PR description now says so.
- Previous finding (repeated
-n): fixed. The delta is the|=change, a comment line, and thetail_n_repeatedcase. - Attacking the fix: 8,600 repeated-
-ncommands compared with GNU tail 9.1 found no regression againstmainor the previous head. The only 4 that differ are the legacytail -5 -n Nform, which behaves the same onmain. - Gates at
1dca4a7:cargo test --workspace --all-targets2733 passed, 0 failed;cargo fmt --checkandcargo clippyexit 0.
I don't have permission to resolve the inline thread, so it is still open on your side. The leading-whitespace and suffix follow-up you mentioned has no issue tracking it yet.
How the fix was checked
- Compared bash with GNU coreutils 9.1,
main(dbb6189), the previous head (c1774a4) and this head, each run as-c '<script>'in an empty directory against a 12-line file. GNU 8.32 was not rerun this round. - Inputs: every ordered pair of 30 count values (plain,
+N,-N, zero, out-of-range line numbers, invalid, empty, 2^64-1 and 2^64) across-n V,-nVand--lines=V, with the file before or after the second option, from a file and from a pipe; plus triples of six values and a few legacy and--forms. - Result: 7,616 already matched GNU, 980 match GNU only since this delta, 4 differ the same way on
main(tail -5 -n +2 fandtail -5 -n 3 fare accepted; GNU exits 1). - Whitespace-padded counts and multiplier suffixes were left out, since those are deferred.
- Not run: Python and Node bindings, wasm build.
Description
When
tail -ngot a count it couldn't parse, it quietly swapped in a default. Sotail -n -2printed the last 10 lines instead of the last 2, andtail -n abcprinted the whole input and exited 0. An agent got a wrong answer with no hint anything was off.Now tail reads the count the same way
headalready does: a leading-means the same as no sign,+Nstill starts at line N, and anything else is an error with status 1, like GNU tail. As in GNU, once any-ncount has a+, later counts are start lines too.Related Issues
Fixes #253
Documentation PR
None
Type of Change
Bug fix
Testing
I added differential cases recorded from bash and GNU coreutils for signed counts, bad counts, and
-ngiven more than once. The first two failed on main and pass with the fix. The third caught a regression in my first version, and it passes now. The full test suite passes.cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.