summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2014-01-09 12:50:20 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2014-01-09 12:50:20 (GMT)
commit5ce3f10aeea711bb912e948fa5d9f63736df1327 (patch)
tree977da819c4d6e15bb355334b15b88b67d8f52c90 /Lib/test
parent78ee3289e991882d803be4cb0dd6b4005f82cbb1 (diff)
downloadcpython-5ce3f10aeea711bb912e948fa5d9f63736df1327.zip
cpython-5ce3f10aeea711bb912e948fa5d9f63736df1327.tar.gz
cpython-5ce3f10aeea711bb912e948fa5d9f63736df1327.tar.bz2
Issue #20078: Reading malformed zipfiles no longer hangs with 100% CPU
consumption.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_zipfile.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index ad0c0b7..a561d59 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -293,6 +293,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)
@@ -389,6 +419,7 @@ class StoredTestsWithSourceFile(AbstractTestsWithSourceFile,
with zipfile.ZipFile(TESTFN2, "w") as zipfp:
self.assertRaises(ValueError, zipfp.write, TESTFN)
+
@requires_zlib
class DeflateTestsWithSourceFile(AbstractTestsWithSourceFile,
unittest.TestCase):