Hi,

Source: coreutils master 75e1b68 (2026-11-18); elide_tail_lines_seekable()
is unchanged since at least 35492ca.

elide_tail_lines_seekable() in src/head.c reads each block with a single
read() and treats the result as the whole block. POSIX allows read() to
return fewer bytes than requested even when more data follows, and this
happens for instance on FUSE files opened with direct I/O. In that case the
missing part of the block is never output, and head exits 0 without a
diagnostic.

To reproduce, I used a FUSE file of 300000 bytes opened with
FOPEN_DIRECT_IO, whose server answers only the first READ request (head's
backward scan at offset 294912, 5088 bytes) with 2544 bytes. "head -n -0"
on that file then prints 297456 bytes instead of 300000, with exit status
0. With full reads, all 300000 bytes are printed.

The attached patch uses full_read() for both reads in that function, as
head already does in its other code paths. With it, the same file is
printed in full, and the head tests pass (7/7). I didn't add a test, as it
needs a filesystem that returns short reads; I can share the FUSE server
script I used if that helps.

Found by an LLM-assisted tool, verified manually.

Regards,
Martin Mirchev
From 0e876f912bd810bf23709c9b84f353f3d26f4c0b Mon Sep 17 00:00:00 2001
From: Martin Mirchev <[email protected]>
Date: Wed, 18 Nov 2026 23:28:19 +0800
Subject: [PATCH] head: -n -N: do not truncate output on short reads from
 seekable files

* src/head.c (elide_tail_lines_seekable): Use full_read instead of
read, as elsewhere in this file.  A short read (fewer bytes than
requested with more data available, which POSIX allows and which
occurs for example on FUSE files opened with direct I/O) was taken as
the whole block, so the bytes missing from that block were silently
dropped from the output and head still exited successfully.
---
 src/head.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/head.c b/src/head.c
index 96c967d..6bfeb57 100644
--- a/src/head.c
+++ b/src/head.c
@@ -666,8 +666,9 @@ elide_tail_lines_seekable (char const *pretty_filename, int fd,
   pos -= bytes_read;
   if (elseek (fd, pos, SEEK_SET, pretty_filename) < 0)
     return false;
-  bytes_read = read (fd, buffer, bytes_read);
-  if (bytes_read < 0)
+  idx_t n_to_read = bytes_read;
+  bytes_read = full_read (fd, buffer, n_to_read);
+  if (bytes_read < n_to_read && errno != 0)
     {
       diagnose_read_failure (pretty_filename);
       return false;
@@ -735,8 +736,8 @@ elide_tail_lines_seekable (char const *pretty_filename, int fd,
       if (elseek (fd, pos, SEEK_SET, pretty_filename) < 0)
         return false;
 
-      bytes_read = read (fd, buffer, BUFSIZ);
-      if (bytes_read < 0)
+      bytes_read = full_read (fd, buffer, BUFSIZ);
+      if (bytes_read < BUFSIZ && errno != 0)
         {
           diagnose_read_failure (pretty_filename);
           return false;
-- 
2.50.1

Reply via email to