Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ffcd339

Browse files
Close #17666: Fix reading gzip files with an extra field.
2 parents c137f7c + 7e69f00 commit ffcd339

3 files changed

Lines changed: 11 additions & 1 deletion

File tree

Lib/gzip.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ def _read_gzip_header(self):
302302

303303
if flag & FEXTRA:
304304
# Read & discard the extra field, if present
305-
self._read_exact(struct.unpack("<H", self._read_exact(2)))
305+
extra_len, = struct.unpack("<H", self._read_exact(2))
306+
self._read_exact(extra_len)
306307
if flag & FNAME:
307308
# Read and discard a null-terminated string containing the filename
308309
while True:

Lib/test/test_gzip.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,13 @@ def test_read_truncated(self):
403403
with gzip.GzipFile(fileobj=io.BytesIO(truncated[:i])) as f:
404404
self.assertRaises(EOFError, f.read, 1)
405405

406+
def test_read_with_extra(self):
407+
# Gzip data with an extra field
408+
gzdata = (b'\x1f\x8b\x08\x04\xb2\x17cQ\x02\xff'
409+
b'\x05\x00Extra'
410+
b'\x0bI-.\x01\x002\xd1Mx\x04\x00\x00\x00')
411+
with gzip.GzipFile(fileobj=io.BytesIO(gzdata)) as f:
412+
self.assertEqual(f.read(), b'Test')
406413

407414
class TestOpen(BaseTest):
408415
def test_binary_modes(self):

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Core and Builtins
3030
Library
3131
-------
3232

33+
- Issue #17666: Fix reading gzip files with an extra field.
34+
3335
- Issue #16475: Support object instancing, recursion and interned strings
3436
in marshal
3537

0 commit comments

Comments
 (0)