On 18/11/2026 06:26, Collin Funk wrote:
* src/tac.c (tac_seekable): Keep track of the offset where the search
for the separator started in G_BUFFER. Use it when setting MATCH_START.
* src/tac.pl ($limits, $INITIAL_READSIZE): New variables.
(@Tests): Add test cases that would previously fail.
* NEWS: Mention the bug fix.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-W4REGHHY.

+1

Discovered by SecBuddyF (with Hy4-preview) of Tencent KeenSecurityLab.

Was the above in reference to https://bugs.gnu.org/82117 ?
If so that link should also be listed.

Also I think this reference the separate github PR?
Link: https://github.com/coreutils/coreutils/pull/469

---
  NEWS             |  5 +++++
  src/tac.c        | 11 +++++++++--
  tests/tac/tac.pl | 23 +++++++++++++++++++++++
  3 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/NEWS b/NEWS
index d2f707c4b..e4cd6d10d 100644
--- a/NEWS
+++ b/NEWS
@@ -305,6 +305,11 @@ GNU coreutils NEWS                                    -*- 
outline -*-
    it would have failed if the control flag encoding was different.
    [This bug was present in "the beginning".]
+ '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.
+  [This bug was present in "the beginning".]
+
    'tail --follow' no longer ignores an overflow of the inotify event queue,
    and now reverts to polling in that case.  Previously tail could fail
    to notice new data, or that a file followed by name was replaced,
diff --git a/src/tac.c b/src/tac.c
index e39b26b8e..84ec6c6c6 100644
--- a/src/tac.c
+++ b/src/tac.c
@@ -254,6 +254,9 @@ tac_seekable (int input_fd, char const *file, off_t 
file_pos)
    if (sentinel_length)
      match_start -= match_length1;
+ /* Offset from G_BUFFER at which the last non-regexp search began. */
+  idx_t search_offset = 0;
+
    while (true)
      {
        /* Search backward from 'match_start' - 1 to 'G_buffer' for a match
@@ -288,6 +291,8 @@ tac_seekable (int input_fd, char const *file, off_t 
file_pos)
          }
        else
          {
+          search_offset = match_start - G_buffer;
+
            /* 'match_length' is constant for non-regexp boundaries. */
            while (*--match_start != first_char
                   || (match_length1 && !STREQ_LEN (match_start + 1, separator1,
@@ -341,9 +346,11 @@ tac_seekable (int input_fd, char const *file, off_t 
file_pos)
               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;
-          /* For non-regexp searches, avoid unnecessary scanning. */
+          /* 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;
+            match_start = G_buffer + read_size + MIN (0, search_offset);
            else
              match_start = past_end;
diff --git a/tests/tac/tac.pl b/tests/tac/tac.pl
index 99d2d0d50..cd97ab10d 100755
--- a/tests/tac/tac.pl
+++ b/tests/tac/tac.pl
@@ -27,6 +27,10 @@ my $bad_dir = 'no/such/dir';
  # This must be longer than 16KiB to trigger the double free in coreutils-8.5.
  my $long_line = 'o' x (16 * 1024 + 1);
+# Keep in sync with the definition from src/tac.c.
+my $limits = getlimits ();
+my $INITIAL_READSIZE = $limits->{BUFSIZ};

I think it best to just set INITIAL_READSIZE to 8192 as per my previous email.


+  # coreutils-9.13 and earlier would crash due to a heap over-read.

9.12

+  ['buf-sep-1', '-s abc',
+   do { my $sep_x = 'x' x $INITIAL_READSIZE;
+        my $sep_y = 'x' x ($INITIAL_READSIZE - 1);
+        ({IN=>$sep_x . '?c'},
+         {IN=>$sep_y . 'ab'},
+         {OUT=>$sep_x . '?c' . $sep_y . 'ab'}); }],
+
+  # coreutils-9.13 and earlier would give invalid output.

ditto.

cheers,
Padraig

Reply via email to