From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Wed, 13 May 2026 10:54:10 +0000 (+0200) Subject: [3.15] bpo-45509: Check gzip headers for corrupted fields (GH-29028) (GH-149769) X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=37f3deb571c02eccd8edc1457fcfc0eeeac909ce;p=thirdparty%2FPython%2Fcpython.git [3.15] bpo-45509: Check gzip headers for corrupted fields (GH-29028) (GH-149769) Check the header checksum it the HCRC field is present. (cherry picked from commit dd94457893a1dd2c99c2405e197f54a7692cbe09) Co-authored-by: Ruben Vorderman --- diff --git a/Lib/gzip.py b/Lib/gzip.py index 971063aa24f8..a89ebf806c85 100644 --- a/Lib/gzip.py +++ b/Lib/gzip.py @@ -484,40 +484,63 @@ def _read_exact(fp, n): return data +def _read_until_null(fp, append_to): + '''Read until the first encountered null byte in fp. + Append to given byte array object''' + while True: + s = fp.read(1) + append_to += s + if not s or s == b'\000': + break + + def _read_gzip_header(fp): '''Read a gzip header from `fp` and progress to the end of the header. Returns last mtime if header was present or None otherwise. ''' magic = fp.read(2) - if magic == b'': + if not magic: return None if magic != b'\037\213': raise BadGzipFile('Not a gzipped file (%r)' % magic) - - (method, flag, last_mtime) = struct.unpack("