From 09cfa445603c4183503ff180444ba38e4b9d3fea Mon Sep 17 00:00:00 2001 From: Tavo Annus Date: Tue, 23 Aug 2022 21:15:39 +0300 Subject: [PATCH] Fix tail panicing when seeking backwards Previously `tail -c n file.txt` caused panic if n > sizeof file.txt. Now it prints out whole file similarly to GNU tail. --- src/uu/tail/src/tail.rs | 4 +++- tests/by-util/test_tail.rs | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/uu/tail/src/tail.rs b/src/uu/tail/src/tail.rs index 32ebb21b06f..a26fe65209e 100644 --- a/src/uu/tail/src/tail.rs +++ b/src/uu/tail/src/tail.rs @@ -1411,7 +1411,9 @@ fn bounded_tail(file: &mut File, settings: &Settings) { file.seek(SeekFrom::Start(i as u64)).unwrap(); } (FilterMode::Bytes(count), false) => { - file.seek(SeekFrom::End(-(*count as i64))).unwrap(); + let len = file.seek(SeekFrom::End(0)).unwrap(); + file.seek(SeekFrom::End(-((*count).min(len) as i64))) + .unwrap(); } (FilterMode::Bytes(count), true) => { // GNU `tail` seems to index bytes and lines starting at 1, not diff --git a/tests/by-util/test_tail.rs b/tests/by-util/test_tail.rs index d4755a48832..6afd55f51d7 100644 --- a/tests/by-util/test_tail.rs +++ b/tests/by-util/test_tail.rs @@ -2435,3 +2435,23 @@ fn test_illegal_seek() { ); assert_eq!(p.wait().unwrap().code().unwrap(), 1); } + +#[test] +fn test_seek_bytes_backward_outside_file() { + new_ucmd!() + .arg("-c") + .arg("100") + .arg(FOOBAR_TXT) + .run() + .stdout_is_fixture(FOOBAR_TXT); +} + +#[test] +fn test_seek_bytes_forward_outside_file() { + new_ucmd!() + .arg("-c") + .arg("+100") + .arg(FOOBAR_TXT) + .run() + .stdout_is(""); +}