coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 3/5] tail: -n -z: fix extra output with short reads from seekable


From: Pádraig Brady
Subject: [PATCH 3/5] tail: -n -z: fix extra output with short reads from seekable input
Date: Wed, 18 Nov 2026 21:18:49 +0000

* src/tail.c (file_lines): Use full_read() to cater for short reads.
* tests/tail/short-read.sh: Add a new test.
* tests/local.mk: Reference the new test.
* NEWS: Mention the bug fix.
---
 NEWS                     |  4 ++++
 src/tail.c               |  9 +++++----
 tests/local.mk           |  1 +
 tests/tail/short-read.sh | 33 +++++++++++++++++++++++++++++++++
 4 files changed, 43 insertions(+), 4 deletions(-)
 create mode 100755 tests/tail/short-read.sh

diff --git a/NEWS b/NEWS
index d38e9e79e..d46cf6311 100644
--- a/NEWS
+++ b/NEWS
@@ -309,6 +309,10 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   it would have failed if the control flag encoding was different.
   [This bug was present in "the beginning".]
 
+  'tail -n N' no longer outputs extra lines when a seekable file returns
+  short reads before end of file.  This also applies to 'tail -z'.
+  [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/tail.c b/src/tail.c
index c67c2546a..477a3de94 100644
--- a/src/tail.c
+++ b/src/tail.c
@@ -37,6 +37,7 @@
 #include "cl-strtod.h"
 #include "dtimespec-bound.h"
 #include "fcntl--.h"
+#include "full-read.h"
 #include "iopoll.h"
 #include "isapipe.h"
 #include "issymlink.h"
@@ -572,8 +573,8 @@ file_lines (char const *prettyname, int fd, struct stat 
const *sb,
   /* Make 'pos' a multiple of 'bufsize' (0 if the file is short), so that all
      reads will be on block boundaries, which might increase efficiency.  */
   pos = xlseek (fd, -bytes_to_read, SEEK_CUR, prettyname);
-  ssize_t bytes_read = read (fd, buffer, bytes_to_read);
-  if (bytes_read < 0)
+  idx_t bytes_read = full_read (fd, buffer, bytes_to_read);
+  if (bytes_read < bytes_to_read && errno != 0)
     {
       pos = -1 - errno;
       error (0, errno, _("error reading %s"), quoteaf (prettyname));
@@ -619,8 +620,8 @@ file_lines (char const *prettyname, int fd, struct stat 
const *sb,
         }
 
       pos = xlseek (fd, -(bufsize + bytes_read), SEEK_CUR, prettyname);
-      bytes_read = read (fd, buffer, bufsize);
-      if (bytes_read < 0)
+      bytes_read = full_read (fd, buffer, bufsize);
+      if (bytes_read < bufsize && errno != 0)
         {
           pos = -1 - errno;
           error (0, errno, _("error reading %s"), quoteaf (prettyname));
diff --git a/tests/local.mk b/tests/local.mk
index 1eb84fa83..42ea050c3 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -289,6 +289,7 @@ all_tests =                                 \
   tests/head/head.pl                           \
   tests/head/head-elide-tail.pl                        \
   tests/tail/tail-sysfs.sh                     \
+  tests/tail/short-read.sh                     \
   tests/tail/tail-n0f.sh                       \
   tests/ls/ls-misc.pl                          \
   tests/date/date.pl                           \
diff --git a/tests/tail/short-read.sh b/tests/tail/short-read.sh
new file mode 100755
index 000000000..2474e26e4
--- /dev/null
+++ b/tests/tail/short-read.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+# Exercise positive short reads in tail's backward scan.
+
+# Copyright (C) 2026 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ tail
+
+file=/sys/kernel/btf/vmlinux
+test -r "$file" || skip_ "cannot read $file"
+
+# The BTF string table ends in NUL-terminated names and has few newlines.
+# Check both the final block and a scan that reaches earlier blocks.
+for opt in -zn1 -n10; do
+  tail ---presume-input-pipe "$opt" "$file" > exp || framework_failure_
+  tail "$opt" "$file" > out || fail=1
+  compare exp out || fail=1
+done
+
+Exit $fail
-- 
2.56.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]