Skip to content

tail: fix -c N printing too much on pipes with short reads - #15845

Open
DevKaurav-14 wants to merge 1 commit into
uutils:mainfrom
DevKaurav-14:fix/tail-bytes-short-reads
Open

DevKaurav-14 wants to merge 1 commit into
uutils:mainfrom
DevKaurav-14:fix/tail-bytes-short-reads

Conversation

@DevKaurav-14

Copy link
Copy Markdown

Closes #15804

When reading from a pipe, BytesChunkBuffer::fill stores each read() as its own chunk but removed at most one old chunk per read. If the first reads are short and later ones are full, the old chunks pile up and get printed, so tail -c 100 could print 819200 bytes.

The fix drops every chunk that is no longer needed after each read (if → while). I added a unit test with a reader that returns 101 one-byte reads followed by full 8 KiB reads; it fails without the change. The reproducer from the issue now prints 100 bytes, and test_tail passes.

This doesn't address the separate memory use from many short reads mentioned in the issue.

BytesChunkBuffer::fill removed at most one old chunk per read, but
reads from a pipe can be short, so chunks differ in size and one full
read can make several old chunks superfluous. The leftovers were then
printed, so `tail -c 100` could output up to 8192 * 100 bytes.

Evict every chunk that is no longer needed after each read, and add a
unit test with a reader that returns short reads followed by full ones.

Closes uutils#15804

This branch has not been deployed

No deployments
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: -c N on a pipe prints up to 8192 x N bytes after short reads

1 participant