Fix tail -c panics when requested bytes exceed file size (#10268)

* Fix tail -c panics when requested bytes exceed file size

* Add tail bytes exceed file size test

* Update test to hit block size condition
This commit is contained in:
Dhruv
2026-01-17 08:26:15 -05:00
committed by GitHub
parent 7eb78ab860
commit 7707b72870
2 changed files with 19 additions and 1 deletions
+3 -1
View File
@@ -473,7 +473,9 @@ fn bounded_tail(file: &mut File, settings: &Settings) {
return;
}
FilterMode::Bytes(Signum::Negative(count)) => {
file.seek(SeekFrom::End(-(*count as i64))).unwrap();
if file.seek(SeekFrom::End(-(*count as i64))).is_err() {
file.seek(SeekFrom::Start(0)).unwrap();
}
limit = Some(*count);
}
FilterMode::Bytes(Signum::Positive(count)) if count > &1 => {
+16
View File
@@ -5040,6 +5040,22 @@ fn tail_n_lines_with_emoji() {
.stdout_only("💐\n");
}
#[test]
fn test_tail_bytes_exceeds_file_size() {
let ts = TestScenario::new(util_name!());
let at = &ts.fixtures;
// Should be > 4096 bytes (block size can vary):
at.write("test_file.txt", &"x".repeat(5000));
ts.ucmd()
.arg("-c")
.arg("1048576")
.arg("test_file.txt")
.succeeds()
.stdout_only("x".repeat(5000));
}
#[test]
#[cfg(target_os = "linux")]
fn test_follow_pipe_f() {