From ede7ba390d1833de8b9290f1a343ccf6c01d43e0 Mon Sep 17 00:00:00 2001 From: harjoth Date: Sat, 20 Jun 2026 20:26:16 -0700 Subject: [PATCH] gh-133998: Clamp out-of-range mtime in tarfile streaming gzip writes tarfile.open(..., "w|gz", mtime=...) packed mtime into the gzip header's 32-bit field with no range check, so a value < 0 or >= 2**32 raised struct.error. Mirror the merged gzip fix: substitute 0 for out-of-range values and coerce to int (RFC 1952), so floats and out-of-range system clocks behave the same as in gzip and the non-streaming "w:gz" path (which already delegates to GzipFile and was unaffected). --- Lib/tarfile.py | 8 +++- Lib/test/test_tarfile.py | 38 +++++++++++++++++++ ...-06-20-20-24-03.gh-issue-133998.nkqhMz.rst | 4 ++ 3 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-06-20-20-24-03.gh-issue-133998.nkqhMz.rst diff --git a/Lib/tarfile.py b/Lib/tarfile.py index a293a0492472749..61909017120d290 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -427,8 +427,12 @@ def _init_write_gz(self, compresslevel, mtime): self.zlib.DEF_MEM_LEVEL, 0) if mtime is None: - mtime = int(time.time()) - timestamp = struct.pack("