From 779f34e180fdcabddb24acc2829410ce8ed50fd1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C3=A1draig=20Brady?= <P@draigBrady.com>
Date: Mon, 31 Jul 2023 12:41:26 +0100
Subject: [PATCH] tac: handle short reads on input

This can be reproduced by getting the read() above 2G,
which induces a short read, thus triggering the erroneous failure.

  $ truncate -s 5G 5G

  $ cat 5G | TMPDIR=$PWD tac | wc -c
  tac: /tmp/tacFt7txA: read error: Illegal seek
  0

With the fix in place we now get:

  $ cat 5G | TMPDIR=$PWD src/tac | wc -c
  5368709120

* src/tac.c (tac_seekable): Use full_read() to handle short reads.
* NEWS: Mention the bug fix.
Reported at https://bugs.debian.org/1042546

Reference:https://github.com/coreutils/coreutils/commit/779f34e180fdcabddb24acc2829410ce8ed50fd1
Conflict:NEWS Context adaptation

---
 NEWS      | 4 ++++
 src/tac.c | 3 ++-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/NEWS b/NEWS
index 41205fa88..2b8f984ba 100644
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,10 @@ GNU coreutils NEWS                                    -*- outline -*-
 * Noteworthy changes in release 9.0 (2021-09-24) [stable]
 
 ** Bug fixes
+  tac now handles short reads on its input.  Previously it may have exited
+  erroneously, especially with large input files with no separators.
+  [This bug was present in "the beginning".]
+
   `wc -c` will again correctly update the read offset of inputs.
   Previously it deduced the size of inputs while leaving the offset unchanged.
   [bug introduced in coreutils-8.27]
diff --git a/src/tac.c b/src/tac.c
index 285f99a74..4c3655895 100644
--- a/src/tac.c
+++ b/src/tac.c
@@ -46,6 +46,7 @@ tac -r -s '.\|
 #include "die.h"
 #include "error.h"
 #include "filenamecat.h"
+#include "full-read.h"
 #include "safe-read.h"
 #include "stdlib--.h"
 #include "xbinary-io.h"
@@ -352,7 +353,7 @@ tac_seekable (int input_fd, char const *file, off_t file_pos)
           else
             match_start = past_end;
 
-          if (safe_read (input_fd, G_buffer, read_size) != read_size)
+          if (full_read (input_fd, G_buffer, read_size) != read_size)
             {
               error (0, errno, _("%s: read error"), quotef (file));
               return false;
-- 
2.27.0

