Here are some simple reproducers for the two semi-related bugs:

    $ head -c $((1024 * 1024 + 8192 + 1)) /dev/zero \
        | head -c -$((1024 * 1024 + 8192)) | wc -c
    8193
    $ head -c $((1024 * 1024 + 8192 + 1)) /dev/zero \
        | head -c -$((1024 * 1024 + 8192 + 2)) | wc -c
    head: error writing 'standard output'
    0

-- 8< --

* src/head.c (elide_tail_bytes_pipe): Don't set REM to BUFSIZ when the
REMAINDER is zero. Don't assume the input is longer than the given
number of bytes.
* tests/head/head-elide-tail.pl ($HEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD)
($MULTIPLE, $FRACTION): New variables.
(@Tests): Add a few test cases for the bugs.
* NEWS: Mention the bug fixes.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-BMLRFV62.
---
 NEWS                          |  8 ++++++++
 src/head.c                    |  4 ++--
 tests/head/head-elide-tail.pl | 29 ++++++++++++++++++++++++++++-
 3 files changed, 38 insertions(+), 3 deletions(-)

diff --git a/NEWS b/NEWS
index f6e1662a4..8b4aba7af 100644
--- a/NEWS
+++ b/NEWS
@@ -130,6 +130,14 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   after a tab character are accounted for correctly.
   [bugs introduced in coreutils-9.8]
 
+  'head --bytes=-N' no longer outputs BUFSIZ extra bytes when N is a multiple 
of
+  BUFSIZ and the input is a pipe with more than 1MiB of data.
+  [bugs introduced in coreutils-9.6]
+
+  'head --bytes=-N' no longer mistakenly fails with a write error N is larger
+  than 1 MiB, and the input is a pipe slightly smaller than N.
+  [bugs introduced in coreutils-5.0.1]
+
   'install --strip' sets the umask to the initial umask specified by the user
   before invoking 'strip' instead of using a umask of zero.
   [This bug was present in "the beginning".]
diff --git a/src/head.c b/src/head.c
index 96c967d8c..fe19dcbe8 100644
--- a/src/head.c
+++ b/src/head.c
@@ -409,7 +409,7 @@ elide_tail_bytes_pipe (char const *filename, int fd, 
uintmax_t n_elide,
         }
 
       /* Output the remainder: rem bytes from b[i] + n_read.  */
-      idx_t rem = READ_BUFSIZE - remainder;
+      idx_t const rem = remainder ? READ_BUFSIZE - remainder : 0;
       if (buffered_enough)
         {
           idx_t n_bytes_left_in_b_i = READ_BUFSIZE - n_read;
@@ -424,7 +424,7 @@ elide_tail_bytes_pipe (char const *filename, int fd, 
uintmax_t n_elide,
               xwrite_stdout (b[i_next], rem - n_bytes_left_in_b_i);
             }
         }
-      else if (i + 1 == n_bufs)
+      else if (i + 1 == n_bufs && READ_BUFSIZE - rem < n_read)
         {
           /* This happens when
              n_elide < file_size < (n_bufs - 1) * READ_BUFSIZE.
diff --git a/tests/head/head-elide-tail.pl b/tests/head/head-elide-tail.pl
index 4ffdcb1a2..03910dd6a 100755
--- a/tests/head/head-elide-tail.pl
+++ b/tests/head/head-elide-tail.pl
@@ -25,9 +25,15 @@ $ENV{PROG} = 'head';
 # Turn off localization of executable's output.
 @ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
 
-# This should match the definition in head.c.
+# These should match the definitions in head.c.
 my $limits = getlimits ();
 my $READ_BUFSIZE = $limits->{BUFSIZ};
+my $HEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD = 1024 * 1024;
+
+# Constants larger than $HEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD, both a multiple
+# and a fraction of $READ_BUFSIZE.
+my $MULTIPLE = $HEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD + $READ_BUFSIZE;
+my $FRACTION = $HEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD + 2;
 
 my @Tests =
   (
@@ -48,6 +54,27 @@ my @Tests =
     {IN=> 'a' x (2 * $READ_BUFSIZE - 2) . 'bcd'},
     {OUT=>'a' x (2 * $READ_BUFSIZE - 2) . 'b'}],
 
+   # coreutils-9.6 through coreutils-9.12 would emit an extra
+   # $READ_BUFSIZE bytes when eliding a multiple of $READ_BUFSIZE
+   # from a pipe.
+   ['elide-b6', "--bytes=-$MULTIPLE ---presume-input-pipe",
+    {IN=> 'a' x ($MULTIPLE - 1)}, {OUT=>''}],
+   ['elide-b7', "--bytes=-$MULTIPLE ---presume-input-pipe",
+    {IN=> 'a' x $MULTIPLE}, {OUT=>''}],
+   ['elide-b8', "--bytes=-$MULTIPLE ---presume-input-pipe",
+    {IN=> 'bcd' . 'a' x $MULTIPLE}, {OUT=>'bcd'}],
+   ['elide-b9', "--bytes=-$MULTIPLE ---presume-input-pipe",
+    {IN=> 'bcd' . 'a' x ($MULTIPLE + $READ_BUFSIZE)},
+    {OUT=>'bcd' . 'a' x $READ_BUFSIZE}],
+
+   # coreutils-5.0.1 through coreutils-9.12 would fail with a write error here.
+   ['elide-b10', "--bytes=-$FRACTION ---presume-input-pipe",
+    {IN=> 'a' x ($FRACTION - 1)}, {OUT=>''}],
+   ['elide-b11', "--bytes=-$FRACTION ---presume-input-pipe",
+    {IN=> 'a' x ($FRACTION - 2)}, {OUT=>''}],
+   ['elide-b12', "--bytes=-$FRACTION ---presume-input-pipe",
+    {IN=> 'bcd' . 'a' x $FRACTION}, {OUT=>'bcd'}],
+
    ['elide-l0', "--lines=-1", {IN=>''}, {OUT=>''}],
    ['elide-l1', "--lines=-1", {IN=>"a\n"}, {OUT=>''}],
    ['elide-l2', "--lines=-1", {IN=>"a"}, {OUT=>''}],
-- 
2.56.1


Reply via email to