]> granicus.if.org Git - python/commitdiff
Close #17666: Fix reading gzip files with an extra field.
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 8 Apr 2013 19:35:02 +0000 (22:35 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 8 Apr 2013 19:35:02 +0000 (22:35 +0300)
Lib/gzip.py
Lib/test/test_gzip.py
Misc/NEWS

index d7da02ca1bec51df33ba074a80479795f460a60e..a1f13b491d3f7ed927b939a82cc8240636f59e93 100644 (file)
@@ -302,7 +302,8 @@ class GzipFile(io.BufferedIOBase):
 
         if flag & FEXTRA:
             # Read & discard the extra field, if present
-            self._read_exact(struct.unpack("<H", self._read_exact(2)))
+            extra_len, = struct.unpack("<H", self._read_exact(2))
+            self._read_exact(extra_len)
         if flag & FNAME:
             # Read and discard a null-terminated string containing the filename
             while True:
index ebd4c43855e41d4e6f25fc94a4ce0d24195b72f3..37fe8538a79bff05b76188741a6a0039854af61a 100644 (file)
@@ -403,6 +403,13 @@ class TestGzip(BaseTest):
             with gzip.GzipFile(fileobj=io.BytesIO(truncated[:i])) as f:
                 self.assertRaises(EOFError, f.read, 1)
 
+    def test_read_with_extra(self):
+        # Gzip data with an extra field
+        gzdata = (b'\x1f\x8b\x08\x04\xb2\x17cQ\x02\xff'
+                  b'\x05\x00Extra'
+                  b'\x0bI-.\x01\x002\xd1Mx\x04\x00\x00\x00')
+        with gzip.GzipFile(fileobj=io.BytesIO(gzdata)) as f:
+            self.assertEqual(f.read(), b'Test')
 
 class TestOpen(BaseTest):
     def test_binary_modes(self):
index 33d9506d8de91753fe6bbef81eab95b1b811f536..969836383a83bbbd7a2d40cbf43da83bdf5e74f4 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #17666: Fix reading gzip files with an extra field.
+
 - Issue #17502: Process DEFAULT values in mock side_effect that returns iterator.
   Patch by Michael Foord.