Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Fix bug where missing NAME or COMMENt fields do not trigger an error
  • Loading branch information
rhpvorderman committed Oct 18, 2021
commit 773c4f173f2a237d20727c0b78aa4f8233796865
8 changes: 4 additions & 4 deletions Lib/gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,14 +442,14 @@ def _read_gzip_header(fp):
if flag & FNAME:
# Read and discard a null-terminated string containing the filename
while True:
s = fp.read(1)
if not s or s==b'\000':
s = _read_exact(fp, 1)
if s == b'\000':
break
if flag & FCOMMENT:
# Read and discard a null-terminated string containing a comment
while True:
s = fp.read(1)
if not s or s==b'\000':
s = _read_exact(fp, 1)
if s == b'\000':
break
if flag & FHCRC:
_read_exact(fp, 2) # Read & discard the 16-bit header CRC
Expand Down
5 changes: 3 additions & 2 deletions Lib/test/test_gzip.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ def test_decompress(self):
datac = gzip.compress(data)
self.assertEqual(gzip.decompress(datac), data)

def test_decompress_uncompressed_header(self):
def test_truncated_header(self):
truncated_headers = [
b"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00", # Missing OS byte
b"\x1f\x8b\x08\x02\x00\x00\x00\x00\x00\xff", # FHRC, but no checksum
Expand All @@ -573,7 +573,8 @@ def test_decompress_uncompressed_header(self):
]
for header in truncated_headers:
with self.subTest(header=header):
self.assertRaises(EOFError, gzip.decompress, header)
self.assertRaises(EOFError, gzip._read_gzip_header,
io.BytesIO(header))

def test_read_truncated(self):
data = data1*50
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Add regression tests for errors that are thrown when decompressing with the
``gzip`` module to ensure backwards-compatibility between Python versions.
Make sure EOFerror is thrown when gzip headers have missing or truncated
NAME or COMMENT fields when FNAME or FCOMMENT flags are set.