mirror of
https://github.com/python/cpython.git
synced 2025-07-19 09:15:34 +00:00
Issue #20078: Reading malformed zipfiles no longer hangs with 100% CPU
consumption.
This commit is contained in:
commit
69fdbf9cb0
3 changed files with 36 additions and 0 deletions
|
@ -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):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue