diff --git a/src/uu/tail/src/tail.rs b/src/uu/tail/src/tail.rs index db1b6a5456..3f591923e1 100644 --- a/src/uu/tail/src/tail.rs +++ b/src/uu/tail/src/tail.rs @@ -480,7 +480,9 @@ 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 seek past EOF may fail; fall back to the end so nothing prints. + file.seek(SeekFrom::Start(*count - 1)) + .or_else(|_| file.seek(SeekFrom::End(0)))?; } _ => {} } diff --git a/tests/by-util/test_tail.rs b/tests/by-util/test_tail.rs index 985d3702ab..0a08d3bf3e 100644 --- a/tests/by-util/test_tail.rs +++ b/tests/by-util/test_tail.rs @@ -1118,6 +1118,16 @@ fn test_positive_zero_bytes() { .no_output(); } +/// A `-c +N` offset past the end of a seekable file must not abort. +#[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() {