Skip to content

tail: stop reading past the original end of file - #15063

Merged
sylvestre merged 1 commit into
uutils:mainfrom
wspperrimh:tail-append-to-input
Oct 5, 2026
Merged

sylvestre merged 1 commit into
uutils:mainfrom
wspperrimh:tail-append-to-input

Conversation

@wspperrimh

Copy link
Copy Markdown
Contributor

tail -n1 f >> f never terminated once the file was larger than the
filesystem block size: bounded_tail copies from the start of the
last lines until EOF, but its own appended output keeps extending the
file, so EOF never arrives and the file grows without bound. GNU tail
9.12 appends the requested lines once and exits.

For FilterMode::Lines(Signum::Negative), bound the forward copy to
the bytes between the seek position and the file size captured when
the file was opened, reusing print_target_section's existing limit
path (take/send_n_bytes). -c N was already bounded by the count,
and the +N modes keep GNU's read-until-EOF semantics.

Adds a regression test that appends tail -n1 output back to a
256 KiB input under an RLIMIT_FSIZE cap, so a broken implementation
fails instead of filling the disk.

Closes #15057.

This patch was written with AI assistance (Devin), as allowed by the
project's AI policy; I reviewed the diff and can justify it in review.

When tail's output is appended back to the file it reads
(`tail -n1 f >> f`), the unbounded forward copy never sees EOF
because its own writes keep extending the file, so it never
terminates and the file grows without bound.

For 'lines from the end' mode, bound the forward copy to the
portion of the file that existed when it was opened, matching
GNU tail, which computes the end position once.
@github-actions

github-actions Bot commented Oct 5, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Note: The gnu test tests/mv/i-3 is now being skipped but was previously passing.
Note: The gnu test tests/stty/bad-speed is now being skipped but was previously passing.
Note: The gnu test tests/stty/stty is now being skipped but was previously passing.
Note: The gnu test tests/stty/stty-invalid is now being skipped but was previously passing.
Note: The gnu test tests/stty/stty-pairs is now being skipped but was previously passing.
Note: The gnu test tests/stty/stty-row-col is now being skipped but was previously passing.

@sylvestre
sylvestre merged commit 781ddfc into uutils:main Oct 5, 2026
130 of 132 checks passed
@xtqqczze

xtqqczze commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

@sylvestre, sorry, but this was merged while the test_output_appended_to_input_file_terminates test was still failing. I propose reverting for now to unblock CI: #15096.

@sylvestre

Copy link
Copy Markdown
Contributor

@wspperrimh sorry, i had to revert this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tail: tail -n1 FILE >> FILE never terminates when FILE is larger than 4096 bytes

3 participants