Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/uu/tail/src/tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,11 @@ fn bounded_tail(file: &mut File, settings: &Settings) -> UResult<()> {
FilterMode::Bytes(Signum::Positive(count)) if count > &1 => {
// GNU `tail` seems to index bytes and lines starting at 1, not
// at 0. It seems to treat `+0` and `+1` as the same thing.
file.seek(SeekFrom::Start(*count - 1)).unwrap();
// A start offset past the end of the file may not be seekable (the
// seek fails instead of landing past EOF); GNU prints nothing then,
// so fall back to the end of the file rather than aborting.
file.seek(SeekFrom::Start(*count - 1))
.or_else(|_| file.seek(SeekFrom::End(0)))?;
}
_ => {}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/by-util/test_tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,19 @@ fn test_positive_zero_bytes() {
.no_output();
}

/// A start byte offset past the end of a regular file must not abort. The file
/// is larger than the block size so the seekable (`bounded_tail`) path is used,
/// and the offset exceeds `i64::MAX` so the seek itself fails; GNU prints
/// nothing in that case instead of crashing.
#[test]
fn test_positive_bytes_past_end_of_file_does_not_panic() {
let (at, mut ucmd) = at_and_ucmd!();
at.write("f", &"a".repeat(1_000_000));
ucmd.args(&["-c", "+18446744073709551615", "f"])
.succeeds()
.no_output();
}

/// Test for reading all but the first NUM lines: `tail -n +3`.
#[test]
fn test_positive_lines() {
Expand Down
Loading