From: Haolin Xue <[email protected]>

In tac_seekable, after the read-ahead memmove that shifts the pending
record to make room for newly read data, match_start was set to
G_buffer + read_size without checking whether the pending record
(saved_record_size) was at least match_length - 1 bytes long.  When
saved_record_size < match_length - 1 (which can happen when processing
a second or later file operand whose final chunk is shorter than the
separator), the backward STREQ_LEN scan can read past past_end into
leftover bytes from a previous file.  If those leftover bytes happen
to match the separator, a spurious match is accepted whose match_end
exceeds past_end, causing output() to underflow bytes_to_add and
read past the G_buffer allocation.

Fix this by capping match_start at past_end - match_length1, so the
STREQ_LEN scan never reads past past_end.  When saved_record_size >=
match_length - 1 (the common case), the cap is a no-op and the
existing optimization is preserved.

* src/tac.c (tac_seekable): Cap match_start at past_end - match_length1
after the read-ahead memmove.
* tests/tac/tac.pl: Add regression test 'oob-multibyte-sep'.

Reported at https://bugs.gnu.org/82117.
Signed-off-by: Haolin Xue <[email protected]>
---
 src/tac.c        | 10 ++++++++--
 tests/tac/tac.pl |  9 +++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/src/tac.c b/src/tac.c
index 1845791ab..e7d731231 100644
--- a/src/tac.c
+++ b/src/tac.c
@@ -341,9 +341,15 @@ 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.
+             Also cap match_start so that the backward STREQ_LEN scan
+             cannot read past past_end: a match starting near the
+             boundary with the pending record would otherwise read
+             beyond the valid data when saved_record_size is less
+             than match_length - 1.  */
           if (sentinel_length)
-            match_start = G_buffer + read_size;
+            match_start = MIN (G_buffer + read_size,
+                               past_end - match_length1);
           else
             match_start = past_end;
 
diff --git a/tests/tac/tac.pl b/tests/tac/tac.pl
index 99d2d0d50..5a9022191 100755
--- a/tests/tac/tac.pl
+++ b/tests/tac/tac.pl
@@ -82,6 +82,15 @@ my @Tests =
 
   # coreutils-8.5's tac would double-free its primary buffer.
   ['double-free', {IN=>$long_line}, {OUT=>$long_line}],
+
+  # bug#82117: with a multi-byte separator (length >= 3) and at least
+  # two file operands, tac could read past the buffer.  When the second
+  # file's pending record (after the read-ahead memmove) is shorter than
+  # match_length - 1, the backward STREQ_LEN scan reads past past_end,
+  # and a spurious match causes output() to underflow bytes_to_add.
+  ['oob-multibyte-sep', '-s XYX',
+   {IN=>'A' x 8193 . 'X'}, {IN=>'A' x 8191 . 'XY'},
+   {OUT=>'A' x 8193 . 'X' . 'A' x 8191 . 'XY'}],
 );
 
 @Tests = triple_test \@Tests;
-- 
2.43.0




Reply via email to