Skip to content

Commit 97e69f0

Browse files
can1357sylvestre
andauthored
head: reset stale partial line state on reused buffers (#11407)
Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
1 parent 32b93b6 commit 97e69f0

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/uu/head/src/take.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ impl TakeAllLinesBuffer {
163163
reader: &mut impl Read,
164164
separator: u8,
165165
) -> std::io::Result<BytesAndLines> {
166+
self.partial_line = false;
166167
let bytes_read = self.inner.fill_buffer(reader)?;
167168
// Count the number of lines...
168169
self.terminated_lines = memchr_iter(separator, self.inner.remaining_buffer()).count();

tests/by-util/test_head.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,35 @@ fn test_all_but_last_bytes_large_file_piped() {
451451
.stdout_only_fixture(seq_19000_file_name);
452452
}
453453

454+
#[test]
455+
fn test_all_but_last_lines_large_file_presume_input_pipe() {
456+
// Validate print-all-but-last-n-lines on the non-seekable path for a large terminated input.
457+
// This input shape forces repeated buffer reuse while some chunks end mid-line.
458+
let scene = TestScenario::new(util_name!());
459+
let fixtures = &scene.fixtures;
460+
461+
let input_file_name = "reused_line_chunks";
462+
let expected_output_file_name = "reused_line_chunks_elide_last";
463+
let line = "aaaaaa\n";
464+
let input_line_count: usize = 20_000;
465+
466+
let mut input = String::with_capacity(line.len() * input_line_count);
467+
for _ in 0..input_line_count {
468+
input.push_str(line);
469+
}
470+
fixtures.write(input_file_name, &input);
471+
fixtures.write(
472+
expected_output_file_name,
473+
&line.repeat(input_line_count - 1),
474+
);
475+
476+
scene
477+
.ucmd()
478+
.args(&["---presume-input-pipe", "-n", "-1", input_file_name])
479+
.succeeds()
480+
.stdout_only_fixture(expected_output_file_name);
481+
}
482+
454483
#[test]
455484
fn test_all_but_last_lines_large_file() {
456485
// Create our fixtures on the fly. We need the input file to be at least double

0 commit comments

Comments
 (0)