]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
tail: don’t output more lines than requested
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 30 Jul 2025 14:49:14 +0000 (07:49 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 4 Aug 2025 02:48:05 +0000 (19:48 -0700)
* src/tail.c (file_lines): Fix an unlikely bug where ‘tail -n N’
could output more than N lines if standard input is a largish
regular file with large initial offset that starts with (say) N-1
lines after the initial offset, but grows to N+1 lines between the
fstat and read calls.  In this case ‘tail -n N’ now outputs N-1
lines, not N+1; that is, it pretends the file grew after ‘tail’
read it.  That is better than outputting more than N lines.

NEWS
src/tail.c

diff --git a/NEWS b/NEWS
index 615d7cb82434cf0e63b3a6672922d19d5f8ba0ba..0bda0eae5954e923bfb5a1c6f0c9fb2ab973540f 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -51,6 +51,11 @@ GNU coreutils NEWS                                    -*- outline -*-
   stdbuf now works on AIX.  Previously it would have been ineffective.
   [bug introduced with the stdbuf program in coreutils-7.5]
 
+  'tail -n NUM' no longer can output more than NUM lines if stdin
+  is a largish regular file with a nonzero initial offset, and grows
+  while 'tail' is reading it.
+  [This bug was present in "the beginning".]
+
   'tail -f -n +NUM' no longer mishandles NUM values >= UINTMAX_MAX
   when the input is seekable.
   [bug introduced in coreutils-9.6]
index 46dee32834f8aba80f30ff41838187f579e3b9f5..28d331781972edf035a6425b744de18047e7059e 100644 (file)
@@ -580,7 +580,7 @@ file_lines (char const *prettyname, int fd, struct stat const *sb,
              start_pos to the end.  */
           xlseek (fd, start_pos, SEEK_SET, prettyname);
           *read_pos = start_pos + dump_remainder (false, prettyname, fd,
-                                                  end_pos);
+                                                  end_pos - start_pos);
           goto free_buffer;
         }
       pos -= bufsize;