Skip to content

Commit 1199d9b

Browse files
committed
Raise EOF error when needed
1 parent d1e7458 commit 1199d9b

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

src/isal/igzip.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,9 @@ def _gzip_header_end(data: bytes) -> int:
270270
if method != 8:
271271
raise BadGzipFile("Unknown compression method")
272272
pos = 10
273-
failure = False
274273
if flags & FEXTRA:
274+
if len(data) < pos + 2:
275+
raise eof_error
275276
xlen = int.from_bytes(data[pos: pos + 2], "little", signed=False)
276277
pos += 2 + xlen
277278
if flags & FNAME:
@@ -286,15 +287,15 @@ def _gzip_header_end(data: bytes) -> int:
286287
raise eof_error
287288
pos = fcomment_end
288289
if flags & FHCRC:
290+
if len(data) < pos + 2:
291+
raise eof_error
289292
header_crc = int.from_bytes(data[pos: pos + 2], "little", signed=False)
290293
# CRC is stored as a 16-bit integer by taking last bits of crc32.
291294
crc = isal_zlib.crc32(data[:pos]) & 0xFFFF
292295
if header_crc != crc:
293296
raise BadGzipFile(f"Corrupted header. Checksums do not "
294297
f"match: {crc} != {header_crc}")
295298
pos += 2
296-
if failure or pos > len(data):
297-
raise eof_error
298299
return pos
299300

300301

tests/test_igzip.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,19 @@ def test_header_corrupt():
272272
igzip.decompress(compressed)
273273
error.match(f"Corrupted header. "
274274
f"Checksums do not match: {true_crc} != {crc}")
275+
276+
277+
TRUNCATED_HEADERS = [
278+
b"\x1f\x8b\x08\x00\x00\x00\x00\x00\x00", # Missing OS byte
279+
b"\x1f\x8b\x08\x02\x00\x00\x00\x00\x00\xff", # FHRC, but no checksum
280+
b"\x1f\x8b\x08\x04\x00\x00\x00\x00\x00\xff", # FEXTRA, but no xlen
281+
b"\x1f\x8b\x08\x04\x00\x00\x00\x00\x00\xff\xaa\x00", # FEXTRA, xlen, but no data # noqa: E501
282+
b"\x1f\x8b\x08\x08\x00\x00\x00\x00\x00\xff", # FNAME but no fname
283+
b"\x1f\x8b\x08\x10\x00\x00\x00\x00\x00\xff", # FCOMMENT, but no fcomment
284+
]
285+
286+
287+
@pytest.mark.parametrize("trunc", TRUNCATED_HEADERS)
288+
def test_truncated_header(trunc):
289+
with pytest.raises(EOFError):
290+
igzip.decompress(trunc)

0 commit comments

Comments
 (0)