From 3f031d431f80668e14f3bc066bbf4369cd9281b9 Mon Sep 17 00:00:00 2001
From: "Miss Islington (bot)"
 <31488909+miss-islington@users.noreply.github.com>
Date: Tue, 23 Jun 2026 15:46:38 +0200
Subject: [PATCH] [3.13] gh-151981: Make tarfile._Stream.seek break at EOF
 (GH-151982) (#151993)

(cherry picked from commit f50bf13566189c8d0ce5a814f33eff3d89951896)

Co-authored-by: Petr Viktorin <encukou@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
Conflict: Adapt context (test file line offset + subTests→subTest for 3.11 compat)
---
 Lib/tarfile.py                                   |  4 +++-
 Lib/test/test_tarfile.py                         | 16 ++++++++++++++++
 ...026-06-23-13-28-16.gh-issue-151981.xBHEcU.rst |  2 ++
 3 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 Misc/NEWS.d/next/Security/2026-06-23-13-28-16.gh-issue-151981.xBHEcU.rst

diff --git a/Lib/tarfile.py b/Lib/tarfile.py
index 2118cf5..bbe746f 100755
--- a/Lib/tarfile.py
+++ b/Lib/tarfile.py
@@ -514,7 +514,9 @@ class _Stream:
         if pos - self.pos >= 0:
             blocks, remainder = divmod(pos - self.pos, self.bufsize)
             for i in range(blocks):
-                self.read(self.bufsize)
+                data = self.read(self.bufsize)
+                if not data:
+                    break
             self.read(remainder)
         else:
             raise StreamError("seeking backwards is not allowed")
diff --git a/Lib/test/test_tarfile.py b/Lib/test/test_tarfile.py
index 5420ef2..5570144 100644
--- a/Lib/test/test_tarfile.py
+++ b/Lib/test/test_tarfile.py
@@ -4455,6 +4455,23 @@ class TestExtractionFilters(unittest.TestCase):
         with self.check_context(arc.open(errorlevel='boo!'), filtererror_filter):
             self.expect_exception(TypeError)  # errorlevel is not int
 
+    def test_getmembers_big_size(self):
+        # gh-151981: A loop in seek() for streaming files tried to read the
+        # declared number of blocks even at EOF
+        for format in (tarfile.GNU_FORMAT, tarfile.PAX_FORMAT):
+            with self.subTest(format=format):
+                tinfo = tarfile.TarInfo("huge-file")
+                tinfo.size = 1 << 64
+                bio = io.BytesIO()
+                # Write header without data
+                bio.write(tinfo.tobuf(format))
+
+                # Reset & try to get contents
+                bio.seek(0)
+                with tarfile.open(fileobj=bio, mode="r|") as tar:
+                    with self.assertRaises(tarfile.ReadError):
+                        tar.getmembers()
+
 
 class OverwriteTests(archiver_tests.OverwriteTests, unittest.TestCase):
     testdir = os.path.join(TEMPDIR, "testoverwrite")
diff --git a/Misc/NEWS.d/next/Security/2026-06-23-13-28-16.gh-issue-151981.xBHEcU.rst b/Misc/NEWS.d/next/Security/2026-06-23-13-28-16.gh-issue-151981.xBHEcU.rst
new file mode 100644
index 0000000..2123ab8
--- /dev/null
+++ b/Misc/NEWS.d/next/Security/2026-06-23-13-28-16.gh-issue-151981.xBHEcU.rst
@@ -0,0 +1,2 @@
+In :mod:`tarfile`, seeking a stream now stops when end of the stream is
+reached.