]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Close #17666: Fix reading gzip files with an extra field.
authorSerhiy Storchaka <storchaka@gmail.com>
Mon, 8 Apr 2013 19:33:55 +0000 (22:33 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Mon, 8 Apr 2013 19:33:55 +0000 (22:33 +0300)
Lib/gzip.py
Lib/test/test_gzip.py
Misc/NEWS

index a2f23679fa8ccca0a48fb045fe7fa58a2eafa080..99b2d3ef078948e1e5010d2fd1cadb5a1bc8040c 100644 (file)
@@ -202,7 +202,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 b0dc24e504efa7f5bcc647dadcd966dd44fa36fa..a8354a93d6627678b417224c37b41ee65337e56e 100644 (file)
@@ -306,6 +306,13 @@ class TestGzip(unittest.TestCase):
             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')
 
 def test_main(verbose=None):
     test_support.run_unittest(TestGzip)
index 57e0cc6575ff6bb181c7f47c7c80a28956600e77..deb0d4e2baeb79fda7cbb7a1d4ee6cc1b7024e19 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -14,6 +14,8 @@ Core and Builtins
 Library
 -------
 
+- Issue #17666: Fix reading gzip files with an extra field.
+
 - Issue #13150, #17512: sysconfig no longer parses the Makefile and config.h
   files when imported, instead doing it at build time.  This makes importing
   sysconfig faster and reduces Python startup time by 20%.