diff --git a/Lib/gzip.py b/Lib/gzip.py index 0dddb51553fabd..57a08b8ea84dc7 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -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 @@ -607,6 +607,9 @@ def decompress(data): do = zlib.decompressobj(wbits=-zlib.MAX_WBITS) # Read all the data except the header 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("