Skip to content

Commit 560b630

Browse files
committed
Address review: drop _ prefixes on test helpers, gate on cpython_only, assert against _EXTHEADER_READ_CHUNK, fix _safe_read docstring
1 parent 38fab8e commit 560b630

2 files changed

Lines changed: 10 additions & 9 deletions

File tree

Lib/tarfile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ def _safe_read(fileobj, size):
268268
"""Read up to *size* bytes from *fileobj* in bounded chunks.
269269
270270
Returns the same bytes as ``fileobj.read(size)`` would (including a short
271-
result at end of file), but never pre-allocates *size* bytes, so an
271+
result at end of file), but limits pre-allocation, so an
272272
oversized size field in a crafted header cannot force a huge allocation.
273273
"""
274274
if size <= _EXTHEADER_READ_CHUNK:

Lib/test/test_tarfile.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ def test_extractfile_attrs(self):
550550
self.assertIs(fobj.seekable(), True)
551551

552552

553-
class _ReadSizeRecorder(io.BytesIO):
553+
class ReadSizeRecorder(io.BytesIO):
554554
# Records the largest size ever passed to read(), so a test can check
555555
# that tarfile does not request far more data than the archive holds
556556
# (which on a real file would pre-allocate it).
@@ -564,36 +564,37 @@ def read(self, size=-1):
564564
return super().read(size)
565565

566566

567+
@support.cpython_only
567568
class ExtendedHeaderMemoryTest(unittest.TestCase):
568569
# gh-151497: the size of a GNU long name/link or a pax extended header is
569570
# read from the archive and is untrusted. A crafted header can claim a
570571
# size far larger than the file actually contains; opening such an archive
571572
# must not try to read (and so pre-allocate) the claimed size in one go.
572573

573-
def _crafted_archive(self, hdrtype):
574+
def crafted_archive(self, hdrtype):
574575
tarinfo = tarfile.TarInfo("A")
575576
tarinfo.type = hdrtype
576577
tarinfo.size = 0xFFFFFFFF # ~4 GiB claimed in a 512-byte header
577578
return tarinfo.tobuf(format=tarfile.GNU_FORMAT)
578579

579-
def _check(self, hdrtype):
580-
fobj = _ReadSizeRecorder(self._crafted_archive(hdrtype))
580+
def check(self, hdrtype):
581+
fobj = ReadSizeRecorder(self.crafted_archive(hdrtype))
581582
try:
582583
with tarfile.open(fileobj=fobj, mode="r:") as tar:
583584
tar.getmembers()
584585
except tarfile.ReadError:
585586
pass # a truncated header is fine; we only check the allocation
586587
# The bogus ~4 GiB size must never reach a single read() call.
587-
self.assertLess(fobj.max_read_size, 10 * 1024 * 1024)
588+
self.assertLessEqual(fobj.max_read_size, tarfile._EXTHEADER_READ_CHUNK)
588589

589590
def test_gnu_longname_oversized_size(self):
590-
self._check(tarfile.GNUTYPE_LONGNAME)
591+
self.check(tarfile.GNUTYPE_LONGNAME)
591592

592593
def test_gnu_longlink_oversized_size(self):
593-
self._check(tarfile.GNUTYPE_LONGLINK)
594+
self.check(tarfile.GNUTYPE_LONGLINK)
594595

595596
def test_pax_header_oversized_size(self):
596-
self._check(tarfile.XHDTYPE)
597+
self.check(tarfile.XHDTYPE)
597598

598599

599600
class MiscReadTestBase(CommonReadTest):

0 commit comments

Comments
 (0)