]> granicus.if.org Git - python/commitdiff
Issue #21560: An attempt to write a data of wrong type no longer cause
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 23 Mar 2015 13:25:43 +0000 (15:25 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 23 Mar 2015 13:25:43 +0000 (15:25 +0200)
GzipFile corruption.  Original patch by Wolfgang Maier.

Lib/gzip.py
Lib/test/test_gzip.py
Misc/NEWS

index f934d4f1c2b389320b6943445a212b85c87e69af..8b122258095f72970b890c88b4b8f4be6cf32791 100644 (file)
@@ -339,9 +339,9 @@ class GzipFile(io.BufferedIOBase):
             data = data.tobytes()
 
         if len(data) > 0:
-            self.size = self.size + len(data)
+            self.fileobj.write(self.compress.compress(data))
+            self.size += len(data)
             self.crc = zlib.crc32(data, self.crc) & 0xffffffff
-            self.fileobj.write( self.compress.compress(data) )
             self.offset += len(data)
 
         return len(data)
index b7a7e03c96ac40599648c211de1a0fc7cdefd8b9..b417044bcbff039094e929f644700d4954f29e58 100644 (file)
@@ -43,6 +43,14 @@ class BaseTest(unittest.TestCase):
 
 
 class TestGzip(BaseTest):
+    def write_and_read_back(self, data, mode='b'):
+        b_data = bytes(data)
+        with gzip.GzipFile(self.filename, 'w'+mode) as f:
+            l = f.write(data)
+        self.assertEqual(l, len(b_data))
+        with gzip.GzipFile(self.filename, 'r'+mode) as f:
+            self.assertEqual(f.read(), b_data)
+
     def test_write(self):
         with gzip.GzipFile(self.filename, 'wb') as f:
             f.write(data1 * 50)
@@ -57,6 +65,31 @@ class TestGzip(BaseTest):
         # Test multiple close() calls.
         f.close()
 
+    # The following test_write_xy methods test that write accepts
+    # the corresponding bytes-like object type as input
+    # and that the data written equals bytes(xy) in all cases.
+    def test_write_memoryview(self):
+        self.write_and_read_back(memoryview(data1 * 50))
+        m = memoryview(bytes(range(256)))
+        data = m.cast('B', shape=[8,8,4])
+        self.write_and_read_back(data)
+
+    def test_write_bytearray(self):
+        self.write_and_read_back(bytearray(data1 * 50))
+
+    def test_write_incompatible_type(self):
+        # Test that non-bytes-like types raise TypeError.
+        # Issue #21560: attempts to write incompatible types
+        # should not affect the state of the fileobject
+        with gzip.GzipFile(self.filename, 'wb') as f:
+            with self.assertRaises(TypeError):
+                f.write('a')
+            with self.assertRaises(TypeError):
+                f.write([1])
+            f.write(data1)
+        with gzip.GzipFile(self.filename, 'rb') as f:
+            self.assertEqual(f.read(), data1)
+
     def test_read(self):
         self.test_write()
         # Try reading.
index 31e6181ce36504020edde378e8d22ad9718c8e4b..103ac4d869361d8167bf50ec0767a2a9cf52dabe 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -18,6 +18,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #21560: An attempt to write a data of wrong type no longer cause
+  GzipFile corruption.  Original patch by Wolfgang Maier.
+
 - Issue #23647: Increase impalib's MAXLINE to accommodate modern mailbox sizes.
 
 - Issue #23539: If body is None, http.client.HTTPConnection.request now sets