mirror of
https://github.com/python/cpython.git
synced 2025-09-20 07:31:10 +00:00
bpo-45507: EOFErrors should be thrown for truncated gzip members (GH-29029)
This commit is contained in:
parent
7e44dc0ba7
commit
0ff3d95b98
3 changed files with 12 additions and 0 deletions
|
@ -603,6 +603,9 @@ def decompress(data):
|
||||||
do = zlib.decompressobj(wbits=-zlib.MAX_WBITS)
|
do = zlib.decompressobj(wbits=-zlib.MAX_WBITS)
|
||||||
# Read all the data except the header
|
# Read all the data except the header
|
||||||
decompressed = do.decompress(data[fp.tell():])
|
decompressed = do.decompress(data[fp.tell():])
|
||||||
|
if not do.eof or len(do.unused_data) < 8:
|
||||||
|
raise EOFError("Compressed file ended before the end-of-stream "
|
||||||
|
"marker was reached")
|
||||||
crc, length = struct.unpack("<II", do.unused_data[:8])
|
crc, length = struct.unpack("<II", do.unused_data[:8])
|
||||||
if crc != zlib.crc32(decompressed):
|
if crc != zlib.crc32(decompressed):
|
||||||
raise BadGzipFile("CRC check failed")
|
raise BadGzipFile("CRC check failed")
|
||||||
|
|
|
@ -562,6 +562,14 @@ class TestGzip(BaseTest):
|
||||||
datac = gzip.compress(data)
|
datac = gzip.compress(data)
|
||||||
self.assertEqual(gzip.decompress(datac), data)
|
self.assertEqual(gzip.decompress(datac), data)
|
||||||
|
|
||||||
|
def test_decompress_truncated_trailer(self):
|
||||||
|
compressed_data = gzip.compress(data1)
|
||||||
|
self.assertRaises(EOFError, gzip.decompress, compressed_data[:-4])
|
||||||
|
|
||||||
|
def test_decompress_missing_trailer(self):
|
||||||
|
compressed_data = gzip.compress(data1)
|
||||||
|
self.assertRaises(EOFError, gzip.decompress, compressed_data[:-8])
|
||||||
|
|
||||||
def test_read_truncated(self):
|
def test_read_truncated(self):
|
||||||
data = data1*50
|
data = data1*50
|
||||||
# Drop the CRC (4 bytes) and file size (4 bytes).
|
# Drop the CRC (4 bytes) and file size (4 bytes).
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Add tests for truncated/missing trailers in gzip.decompress implementation.
|
Loading…
Add table
Add a link
Reference in a new issue