* src/tac.c (tac_seekable): Ensure read_size and G_buffer_size
stay in sync.
* tests/tac/tac.pl: Add a test case.
* NEWS: Mention the bug fix.
Discovered by Anthropic's OSS Scanner, as bug ANT-2026-L2U0MKDU.
---
NEWS | 4 ++++
src/tac.c | 20 ++++++++------------
tests/tac/tac.pl | 7 +++++++
3 files changed, 19 insertions(+), 12 deletions(-)
diff --git a/NEWS b/NEWS
index 925718fa6..01360563e 100644
--- a/NEWS
+++ b/NEWS
@@ -389,6 +389,10 @@ GNU coreutils NEWS -*-
outline -*-
their contents, or the edge case of seekable files that return short reads.
[bug introduced in coreutils-8.24]
+ 'tac' no longer reports "memory exhausted" for some combinations of
+ input files containing long lines.
+ [bug introduced in coreutils-5.3.0]
+
'tac -s SEP' with a multi-byte SEP no longer reads past the end of an
internal
buffer. Previously, it could result in incorrect output or a crash when
given
multiple inputs.
diff --git a/src/tac.c b/src/tac.c
index 0b2c37a0d..61d56188e 100644
--- a/src/tac.c
+++ b/src/tac.c
@@ -331,30 +331,26 @@ tac_seekable (int input_fd, char const *file, off_t
file_pos)
G_buffer = newbuffer;
}
- /* Back up to the start of the next bufferfull of the file. */
- if (file_pos >= read_size)
- file_pos -= read_size;
- else
- {
- read_size = file_pos;
- file_pos = 0;
- }
+ /* A short block at the start of this file does not change
+ the buffer size used for subsequent files. */
+ idx_t n_to_read = MIN (file_pos, read_size);
+ file_pos -= n_to_read;
if (lseek (input_fd, file_pos, SEEK_SET) < 0)
error (0, errno, _("%s: seek failed"), quotef (file));
/* Shift the pending record data right to make room for the new.
The source and destination regions probably overlap. */
- memmove (G_buffer + read_size, G_buffer, saved_record_size);
- past_end = G_buffer + read_size + saved_record_size;
+ memmove (G_buffer + n_to_read, G_buffer, saved_record_size);
+ past_end = G_buffer + n_to_read + saved_record_size;
/* For non-regexp searches, avoid unnecessary scanning.
Keep our position before pending data, so a match doesn't
extend beyond PAST_END or overlap with the previous match. */
if (sentinel_length)
- match_start = G_buffer + read_size + MIN (0, search_offset);
+ match_start = G_buffer + n_to_read + MIN (0, search_offset);
else
match_start = past_end;
- if (full_read (input_fd, G_buffer, read_size) != read_size)
+ if (full_read (input_fd, G_buffer, n_to_read) != n_to_read)
{
error (0, errno, _("%s: read error"), quotef (file));
return false;
diff --git a/tests/tac/tac.pl b/tests/tac/tac.pl
index 4072919a7..db4f9feae 100755
--- a/tests/tac/tac.pl
+++ b/tests/tac/tac.pl
@@ -86,6 +86,13 @@ my @Tests =
# coreutils-8.5's tac would double-free its primary buffer.
['double-free', {IN=>$long_line}, {OUT=>$long_line}],
+ # In coreutils v5.3.0 to v9.12 a short block at the start of the first file
+ # used to make tac report "memory exhausted" while processing the second
file.
+ ['long-lines',
+ do { my $a = 'a' x (5 * $INITIAL_READSIZE - 1) . "\n";
+ my $b = 'b' x (3 * $INITIAL_READSIZE - 1) . "\n";
+ ({IN=>$a}, {IN=>$b}, {OUT=>$a . $b}); }],
+
# coreutils-9.12 and earlier would crash due to a heap over-read.
['buf-sep-1', '-s abc',
do { my $sep_x = 'x' x $INITIAL_READSIZE;
--
2.56.0