* src/tail.c (file_lines): Ensure the position is updated on each read.
* tests/tail/truncate.sh: Increase the input size to exercise
multiple reads.  Also check rotation is detected.
* NEWS: Mention the bug fix.

Discovered by Anthropic's OSS Scanner, as bug ANT-2026-DA5D4S3D.
---
 NEWS                   |  5 +++++
 src/tail.c             |  2 +-
 tests/tail/truncate.sh | 22 ++++++++++++++++++++--
 3 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/NEWS b/NEWS
index d46cf6311..781db526e 100644
--- a/NEWS
+++ b/NEWS
@@ -313,6 +313,11 @@ GNU coreutils NEWS                                    -*- 
outline -*-
   short reads before end of file.  This also applies to 'tail -z'.
   [This bug was present in "the beginning".]
 
+  'tail -f F' now correctly detect truncation and file replacement.
+  Previously -f could miss file truncation, and when polling,
+  -F could miss file replacement.
+  [bug introduced in coreutils-9.9]
+
   '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 477a3de94..188024541 100644
--- a/src/tail.c
+++ b/src/tail.c
@@ -615,7 +615,7 @@ file_lines (char const *prettyname, int fd, struct stat 
const *sb,
           /* Not enough lines in the file; print everything from
              start_pos to the end.  */
           xwrite_stdout (buffer, bytes_read);
-          dump_remainder (false, prettyname, fd, end_pos - pos);
+          pos += dump_remainder (false, prettyname, fd, end_pos - pos);
           goto free_buffer;
         }
 
diff --git a/tests/tail/truncate.sh b/tests/tail/truncate.sh
index a02d7f88c..9213e8cee 100755
--- a/tests/tail/truncate.sh
+++ b/tests/tail/truncate.sh
@@ -1,5 +1,5 @@
 #!/bin/sh
-# Ensure all logs are output upon file truncation
+# Ensure tail follows files across truncation and replacement.
 
 # Copyright (C) 2015-2026 Free Software Foundation, Inc.
 
@@ -18,6 +18,7 @@
 
 . "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
 print_ver_ tail
+getlimits_
 
 check_tail_output()
 {
@@ -32,10 +33,15 @@ cleanup_() { kill "$pid" 2>/dev/null; wait "$pid" 
2>/dev/null; pid=; }
 # Speedup the non inotify case
 fastpoll='-s.1 --max-unchanged-stats=1'
 
+# Start with fewer than 10 lines. Pick a larger size to ensure
+# we test multiple reads.  Note BUFSIZ currently suffices but we
+# use IO_BUFSIZE to decouple from the implementation somewhat.
+printf '%*s\n10\n' "$(($IO_BUFSIZE * 2))" '' > big || framework_failure_
+
 for follow in '-f' '-F'; do
   for mode in '' '---disable-inotify'; do
     rm -f out
-    seq 10 > f || framework_failure_
+    cp big f || framework_failure_
 
     tail $follow $mode $fastpoll f > out 2>&1 & pid=$!
 
@@ -43,6 +49,18 @@ for follow in '-f' '-F'; do
     tail_re='^10$' retry_delay_ check_tail_output .1 7 ||
       { cat out; fail=1; }
 
+    # coreutils 9.9-9.12 had a stale read position in the first buffer
+    # which hid truncation, and also kept polling tail -F from rechecking
+    # the file name, so check that also.
+    if test "$follow" = '-f'; then
+      truncate -s "$IO_BUFSIZE" f || framework_failure_
+    else
+      printf '%*s' "$IO_BUFSIZE" '' > new && mv -f new f || framework_failure_
+    fi
+    printf '\nchanged\n' >> f || framework_failure_
+    tail_re='^changed$' retry_delay_ check_tail_output .1 7 ||
+      { tail -c 256 out; fail=1; }
+
     seq 11 15 > f || framework_failure_
 
     # Wait up to 12.7s for new data
-- 
2.56.0


Reply via email to