tail: don't panic on '-c -N' with a huge N - #14372
Open
harshasiddartha wants to merge 1 commit into
Open
Conversation
`bounded_tail` computed the backwards seek offset for `-c -N` as `-(count as i64)`. For N of 2^63 that cast yields `i64::MIN`, whose negation overflows: a build with overflow checks aborts with exit 101, and a release build wraps around and seeks to a bogus position. Convert the count to an `i64` first and, when it does not fit, take the existing path for an offset that lands before the start of the file.
|
GNU testsuite comparison: |
sylvestre
reviewed
Sep 4, 2026
Comment on lines
+472
to
+474
| // A count above `i64::MAX` has no negative counterpart to seek by, | ||
| // and reaches further back than any file can be long, so treat it | ||
| // like any other offset landing before the start of the file. |
Contributor
There was a problem hiding this comment.
can you write this as one line?
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 #14353.
tail -c -9223372036854775808 FILEon a seekable file larger than the block size aborts withattempt to negate with overflow(exit 101) in a build with overflow checks, and wraps silently in a release build.bounded_tailbuilt the backwards seek offset for-c -Nas-(*count as i64).countis au64, so2^63casts toi64::MIN, and negatingi64::MINoverflows.I now convert the count with
i64::try_frombefore negating it. A count that doesn't fit in ani64reaches further back than any file can be long, so it falls into the branch the arm already had for an offset landing before the start of the file: seek to 0 and print the whole file. That matches what-c -Nalready does for any other N larger than the file.Tested with a new test in
tests/by-util/test_tail.rsthat runs-c -9223372036854775808against an 8 KiB file (larger thansane_blksize, so the seek path is taken) and expects the whole file back. It fails with exit 101 before the change and passes after. I also ran the rest oftest_tailandcargo clippy -p uu_tail. Everything was run on macOS only;test_follow_detects_file_truncationis flaky here under load, but it fails the same way on an unmodified checkout.