Skip to content

Commit 83e58a2

Browse files
committed
Throw EOF error when trailer is not present in gzip member
This is to keep error compatibility with 3.10 and lower.
1 parent fd03917 commit 83e58a2

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

Lib/gzip.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,9 @@ def decompress(data):
607607
do = zlib.decompressobj(wbits=-zlib.MAX_WBITS)
608608
# Read all the data except the header
609609
decompressed = do.decompress(data[fp.tell():])
610+
if not do.eof or len(do.unused_data) < 8:
611+
raise EOFError("Compressed file ended before the end-of-stream "
612+
"marker was reached")
610613
crc, length = struct.unpack("<II", do.unused_data[:8])
611614
if crc != zlib.crc32(decompressed):
612615
raise BadGzipFile("CRC check failed")

Lib/test/test_gzip.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,19 @@ def test_decompress(self):
562562
datac = gzip.compress(data)
563563
self.assertEqual(gzip.decompress(datac), data)
564564

565+
def test_decompress_uncompressed_header(self):
566+
truncated_headers = [
567+
b"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00", # Missing OS byte
568+
b"\x1f\x8b\x08\x02\x00\x00\x00\x00\x00\xff", # FHRC, but no checksum
569+
b"\x1f\x8b\x08\x04\x00\x00\x00\x00\x00\xff", # FEXTRA, but no xlen
570+
b"\x1f\x8b\x08\x04\x00\x00\x00\x00\x00\xff\xaa\x00", # FEXTRA, xlen, but no data
571+
b"\x1f\x8b\x08\x08\x00\x00\x00\x00\x00\xff", # FNAME but no fname
572+
b"\x1f\x8b\x08\x10\x00\x00\x00\x00\x00\xff", # FCOMMENT, but no fcomment
573+
]
574+
for header in truncated_headers:
575+
with self.subTest(header=header):
576+
self.assertRaises(EOFError, gzip.decompress, header)
577+
565578
def test_read_truncated(self):
566579
data = data1*50
567580
# Drop the CRC (4 bytes) and file size (4 bytes).
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Add regression tests for errors that are thrown when decompressing with the
2+
``gzip`` module to ensure backwards-compatibility between Python versions.

0 commit comments

Comments
 (0)