]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #20078: Reading malformed zipfiles no longer hangs with 100% CPU
authorSerhiy Storchaka <storchaka@gmail.com>
Thu, 9 Jan 2014 12:50:20 +0000 (14:50 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Thu, 9 Jan 2014 12:50:20 +0000 (14:50 +0200)
consumption.

Lib/test/test_zipfile.py
Lib/zipfile.py
Misc/NEWS

index ad0c0b7b41680882b1eba5263deaa85b1952f6a4..a561d59bc7684077b70ee564b999b7b6fdbaa6dd 100644 (file)
@@ -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):
index 92c98abe1eb47aad7fab2bdf039a092e826275a9..a1b34147f835c68d087ab927936b96e20cf78f3c 100644 (file)
@@ -860,6 +860,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))
index 4ad230af6fed5609823869e34d421f4dbe177d2e..afe8514fa70141721bc56197d9c51384f6850dfc 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -36,6 +36,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #20078: Reading malformed zipfiles no longer hangs with 100% CPU
+  consumption.
+
 - Issue #20113: os.readv() and os.writev() now raise an OSError exception on
   error instead of returning -1.