diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2014-01-09 12:53:41 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2014-01-09 12:53:41 (GMT) |
commit | 69fdbf9cb02b09fdebe3ce13ec3be56d69657a3c (patch) | |
tree | caeb7277649d381846ee1a69f1e30298e8c50eee /Lib | |
parent | 0ae7ae1fc9fe8891ad3daf26a2dec3967600a7e7 (diff) | |
parent | 5ce3f10aeea711bb912e948fa5d9f63736df1327 (diff) | |
download | cpython-69fdbf9cb02b09fdebe3ce13ec3be56d69657a3c.zip cpython-69fdbf9cb02b09fdebe3ce13ec3be56d69657a3c.tar.gz cpython-69fdbf9cb02b09fdebe3ce13ec3be56d69657a3c.tar.bz2 |
Issue #20078: Reading malformed zipfiles no longer hangs with 100% CPU
consumption.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_zipfile.py | 31 | ||||
-rw-r--r-- | Lib/zipfile.py | 2 |
2 files changed, 33 insertions, 0 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py index 49eccbb..33602cf 100644 --- a/Lib/test/test_zipfile.py +++ b/Lib/test/test_zipfile.py @@ -297,6 +297,36 @@ class AbstractTestsWithSourceFile: buf = fp.read(test_size) self.assertEqual(len(buf), test_size) + def test_truncated_zipfile(self): + fp = io.BytesIO() + with zipfile.ZipFile(fp, mode='w') as zipf: + zipf.writestr('strfile', self.data, compress_type=self.compression) + end_offset = fp.tell() + zipfiledata = fp.getvalue() + + fp = io.BytesIO(zipfiledata) + with zipfile.ZipFile(fp) as zipf: + with zipf.open('strfile') as zipopen: + fp.truncate(end_offset - 20) + with self.assertRaises(EOFError): + zipopen.read() + + fp = io.BytesIO(zipfiledata) + with zipfile.ZipFile(fp) as zipf: + with zipf.open('strfile') as zipopen: + fp.truncate(end_offset - 20) + with self.assertRaises(EOFError): + while zipopen.read(100): + pass + + fp = io.BytesIO(zipfiledata) + with zipfile.ZipFile(fp) as zipf: + with zipf.open('strfile') as zipopen: + fp.truncate(end_offset - 20) + with self.assertRaises(EOFError): + while zipopen.read1(100): + pass + def tearDown(self): unlink(TESTFN) unlink(TESTFN2) @@ -393,6 +423,7 @@ class StoredTestsWithSourceFile(AbstractTestsWithSourceFile, with zipfile.ZipFile(TESTFN2, "w") as zipfp: self.assertRaises(ValueError, zipfp.write, TESTFN) + @requires_zlib class DeflateTestsWithSourceFile(AbstractTestsWithSourceFile, unittest.TestCase): diff --git a/Lib/zipfile.py b/Lib/zipfile.py index 173d990..12c1754 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -862,6 +862,8 @@ class ZipExtFile(io.BufferedIOBase): data = self._fileobj.read(n) self._compress_left -= len(data) + if not data: + raise EOFError if self._decrypter is not None: data = bytes(map(self._decrypter, data)) |