Skip to content

Commit

Permalink
Merge pull request #3873 from kilpkonn/tail_seek_outside_bounds
Browse files Browse the repository at this point in the history
Fix tail panicing when seeking backwards
  • Loading branch information
sylvestre authored Aug 26, 2022
2 parents 72d7120 + 09cfa44 commit 0dbcdde
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/uu/tail/src/tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/by-util/test_tail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
}

0 comments on commit 0dbcdde

Please sign in to comment.