Skip to content

tail: don't panic on '-c +N' with a huge N - #14034

Open
nagendramohan wants to merge 1 commit into
uutils:mainfrom
nagendramohan:fix/tail-positive-bytes-seek-overflow-13887
Open

tail: don't panic on '-c +N' with a huge N#14034
nagendramohan wants to merge 1 commit into
uutils:mainfrom
nagendramohan:fix/tail-positive-bytes-seek-overflow-13887

Conversation

@nagendramohan

Copy link
Copy Markdown

tail -c +N FILE on a regular file larger than the block size seeks to byte N-1. When N-1 exceeds what the OS can seek to (any offset above i64::MAX always fails with EINVAL), the .unwrap() aborted the process.

Clamp the start offset to the file length. A start beyond the end of the file produces no output (matching GNU tail), and clamping also avoids seeking to an offset the OS cannot represent — so the pathological case yields empty output instead of panicking. Behavior is unchanged for valid offsets.

Adds a regression test (verified failing before the fix). Closes #13887.

Closes #13887.

tail -c +N FILE on a regular file larger than the block size seeks to byte N-1. When N-1 exceeds what the OS can seek to (any offset above i64::MAX fails with EINVAL), the .unwrap() aborted the process:

$ yes | head -c 8192 > big
$ tail -c +18446744073709551615 big # -> SIGABRT / exit 134

Fix: clamp the start offset to the file length. A start beyond EOF produces no output (matching GNU tail), and clamping avoids the un-seekable offset
entirely. Behavior is unchanged for valid offsets.

Adds a regression test (verified failing before the fix). cargo fmt, cargo clippy, and the tail test suite pass.

`tail -c +N FILE` on a regular file larger than the block size seeks to
byte N-1. When N-1 exceeds what the OS can seek to (any offset above
i64::MAX always fails with EINVAL), the `.unwrap()` aborted the process.

Clamp the start offset to the file length. A start beyond the end of the
file produces no output (matching GNU `tail`), and clamping also avoids
seeking to an offset the OS cannot represent — so the pathological case
yields empty output instead of panicking. Behavior is unchanged for
valid offsets.

Adds a regression test (verified failing before the fix). Closes uutils#13887.
Comment thread src/uu/tail/src/tail.rs
// at 0. It seems to treat `+0` and `+1` as the same thing.
file.seek(SeekFrom::Start(*count - 1)).unwrap();
//
// Clamp the start offset to the file length. A start beyond the end of the file

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need 4 lines comme r for this

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.

tail panics (aborts) on -c +N with a very large N (seek .unwrap() on EINVAL)

2 participants