* src/tac.c (tac_seekable): Use full_read() to handle short reads.
* tests/tac/tac-2-nonseekable.sh: Add test cases.
* NEWS: Mention the bug fix.
Discovered by Anthropic's OSS Scanner, as bug ANT-2026-TXAVHLCL.
---
NEWS | 4 ++++
src/tac.c | 13 ++++++-------
tests/tac/tac-2-nonseekable.sh | 14 +++++++++++++-
3 files changed, 23 insertions(+), 8 deletions(-)
diff --git a/NEWS b/NEWS
index 781db526e..38a746208 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".]
+ 'tac' no longer silently drops data from seekable files that return
+ short reads or report a size smaller than their contents.
+ [bug introduced in coreutils-8.24]
+
'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".]
diff --git a/src/tac.c b/src/tac.c
index 1845791ab..6f0db56e2 100644
--- a/src/tac.c
+++ b/src/tac.c
@@ -222,8 +222,8 @@ tac_seekable (int input_fd, char const *file, off_t
file_pos)
/* Scan backward, looking for end of file. This caters to proc-like
file systems where the file size is just an estimate. */
- while ((saved_record_size = read (input_fd, G_buffer, read_size)) == 0
- && file_pos != 0)
+ while ((saved_record_size = full_read (input_fd, G_buffer, read_size)) == 0
+ && errno == 0 && file_pos != 0)
{
off_t rsize = read_size;
if (lseek (input_fd, -rsize, SEEK_CUR) < 0)
@@ -234,16 +234,15 @@ tac_seekable (int input_fd, char const *file, off_t
file_pos)
/* Now scan forward, looking for end of file. */
while (saved_record_size == read_size)
{
- ssize_t nread = read (input_fd, G_buffer, read_size);
+ idx_t nread = full_read (input_fd, G_buffer, read_size);
if (nread == 0)
break;
+ file_pos += read_size;
saved_record_size = nread;
- if (saved_record_size < 0)
- break;
- file_pos += nread;
}
- if (saved_record_size < 0)
+ /* The last full_read was short. errno distinguishes EOF from error. */
+ if (errno != 0)
{
error (0, errno, _("%s: read error"), quotef (file));
return false;
diff --git a/tests/tac/tac-2-nonseekable.sh b/tests/tac/tac-2-nonseekable.sh
index f96efe98d..24a9412eb 100755
--- a/tests/tac/tac-2-nonseekable.sh
+++ b/tests/tac/tac-2-nonseekable.sh
@@ -25,8 +25,9 @@ compare exp out || fail=1
compare /dev/null err || fail=1
# Make sure it works on funny files in /proc and /sys.
+# The BTF file can return a short read before EOF.
-for file in /proc/version /sys/kernel/profiling; do
+for file in /proc/version /sys/kernel/profiling /sys/kernel/btf/vmlinux; do
if test -r $file; then
cp -f $file copy &&
tac copy > exp1 || framework_failure_
@@ -36,6 +37,17 @@ for file in /proc/version /sys/kernel/profiling; do
fi
done
+# On Linux, SEEK_END on /proc/self/environ returns 0.
+# Give tac an environment entry likely to span a full read buffer.
+if test -r /proc/self/environ; then
+ tac_exe=$(cmd_exe tac) &&
+ entry=$(printf 'PAD=%09000d' 0) || framework_failure_
+
+ env -i "$entry" "$tac_exe" /proc/self/environ > out || fail=1
+ # Count the entry and its terminating NUL, allowing extras added on exec.
+ test "$(wc -c < out)" -ge "$(printf '%s\0' "$entry" | wc -c)" || fail=1
+fi
+
# Assume timeout is due to failure to close stdin with <&-
# which was seen on NetBSD 7.1 / x86_64
returns_ 124 timeout 10 tac - - <&- 2>err && skip_ 'error closing stdin'
--
2.56.0