Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Lib/test/test_zipfile/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4998,6 +4998,43 @@ def test_read1_output_is_bounded(self):
with zf.open("big") as f:
self.assertLessEqual(len(f._read1(100)), f.MIN_READ_SIZE)

def test_read_stops_at_file_size(self):
# read() without a size returns no more than the file_size of the
# member, and should not decompress the rest of it to get there.
data = b"\0" * (4 * 1024 * 1024)
buf = io.BytesIO()
with zipfile.ZipFile(buf, "w", compression=self.compression) as zf:
zf.writestr("big", data)

class Recorder:
def __init__(self, decompressor):
self.decompressor = decompressor
self.largest = 0

def decompress(self, data, max_length):
data = self.decompressor.decompress(data, max_length)
self.largest = max(self.largest, len(data))
return data

def __getattr__(self, name):
return getattr(self.decompressor, name)

for read in "read", "read1":
with self.subTest(read=read):
with zipfile.ZipFile(io.BytesIO(buf.getvalue())) as zf:
zinfo = zf.getinfo("big")
zinfo.file_size = 100
zinfo.CRC = zipfile.crc32(data[:100])
with zf.open(zinfo) as f:
if f._decompressor is not None:
f._decompressor = recorder = Recorder(f._decompressor)
else:
recorder = None
self.assertEqual(getattr(f, read)(-1), data[:100])
self.assertEqual(f.read(), b"")
if recorder is not None:
self.assertLessEqual(recorder.largest, f.MIN_READ_SIZE)


class StoredBoundedDecompressTests(AbstractBoundedDecompressTests,
unittest.TestCase):
Expand Down
9 changes: 6 additions & 3 deletions Lib/zipfile/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,11 +1206,14 @@ def _read1(self, n):
else:
data = b''

# Do not decompress more than the member still has to give: read()
# asks for MAX_N, and the rest is dropped below anyway.
max_length = max(min(n, self._left), self.MIN_READ_SIZE)

if self._compress_type == ZIP_STORED:
self._eof = self._compress_left <= 0
elif self._compress_type == ZIP_DEFLATED:
n = max(n, self.MIN_READ_SIZE)
data = self._decompressor.decompress(data, n)
data = self._decompressor.decompress(data, max_length)
self._eof = (self._decompressor.eof or
self._compress_left <= 0 and
not self._decompressor.unconsumed_tail)
Expand All @@ -1221,7 +1224,7 @@ def _read1(self, n):
# DEFLATE path above) so that a small compressed member cannot
# expand into one unbounded read.
try:
data = self._decompressor.decompress(data, max(n, self.MIN_READ_SIZE))
data = self._decompressor.decompress(data, max_length)
except TypeError:
# See MonkeypatchedDecompressorTests in test_core.py
warnings._deprecated(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:meth:`zipfile.ZipFile.read` and :meth:`!zipfile.ZipExtFile.read` without a
size no longer decompress a member past its :attr:`~zipfile.ZipInfo.file_size`
before returning. An archive that states a smaller size than its data
expands to made them use memory for all of it.
Loading