Skip to content

Commit 7f0dc59

Browse files
miss-islingtonencukouStanFromIreland
authored
[3.11] gh-151981: Make tarfile._Stream.seek break at EOF (GH-151982) (#151995)
gh-151981: Make tarfile._Stream.seek break at EOF (GH-151982) (cherry picked from commit f50bf13) Co-authored-by: Petr Viktorin <encukou@gmail.com> Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent 79f6614 commit 7f0dc59

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

Lib/tarfile.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,9 @@ def seek(self, pos=0):
515515
if pos - self.pos >= 0:
516516
blocks, remainder = divmod(pos - self.pos, self.bufsize)
517517
for i in range(blocks):
518-
self.read(self.bufsize)
518+
data = self.read(self.bufsize)
519+
if not data:
520+
break
519521
self.read(remainder)
520522
else:
521523
raise StreamError("seeking backwards is not allowed")

Lib/test/test_tarfile.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4373,6 +4373,22 @@ def valueerror_filter(tarinfo, path):
43734373
with self.check_context(arc.open(errorlevel='boo!'), filtererror_filter):
43744374
self.expect_exception(TypeError) # errorlevel is not int
43754375

4376+
@support.subTests('format', [tarfile.GNU_FORMAT, tarfile.PAX_FORMAT])
4377+
def test_getmembers_big_size(self, format):
4378+
# gh-151981: A loop in seek() for streaming files tried to read the
4379+
# declared number of blocks even at EOF
4380+
tinfo = tarfile.TarInfo("huge-file")
4381+
tinfo.size = 1 << 64
4382+
bio = io.BytesIO()
4383+
# Write header without data
4384+
bio.write(tinfo.tobuf(format))
4385+
4386+
# Reset & try to get contents
4387+
bio.seek(0)
4388+
with tarfile.open(fileobj=bio, mode="r|") as tar:
4389+
with self.assertRaises(tarfile.ReadError):
4390+
tar.getmembers()
4391+
43764392

43774393
class OverwriteTests(archiver_tests.OverwriteTests, unittest.TestCase):
43784394
testdir = os.path.join(TEMPDIR, "testoverwrite")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
In :mod:`tarfile`, seeking a stream now stops when end of the stream is
2+
reached.

0 commit comments

Comments
 (0)