From: Martin Mirchev <[email protected]>

* 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.
* NEWS: Mention the bug fix.
Fixes https://bugs.gnu.org/82270
---
 NEWS       | 4 ++++
 src/head.c | 9 +++++----
 2 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/NEWS b/NEWS
index 1469e74de..d38e9e79e 100644
--- a/NEWS
+++ b/NEWS
@@ -138,6 +138,10 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   and the input is a pipe slightly smaller than N.
   [bugs introduced in coreutils-5.0.1]
 
+  'head --lines=-N' no longer silently drops data in the unlikely case
+  where a seekable file returns short reads before end of file.
+  [bug 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 fe19dcbe8..2ce8e4ffa 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.56.0


Reply via email to