Skip to content

Commit 64681af

Browse files
committed
pythongh-133998: Fix gzip file creation when time is out of range
1 parent 3fa30d9 commit 64681af

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

‎Doc/library/gzip.rst‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,12 @@ The module defines the following items:
101101
is no compression. The default is ``9``.
102102

103103
The optional *mtime* argument is the timestamp requested by gzip. The time
104-
is in Unix format, i.e., seconds since 00:00:00 UTC, January 1, 1970.
105-
If *mtime* is omitted or ``None``, the current time is used. Use *mtime* = 0
106-
to generate a compressed stream that does not depend on creation time.
104+
is in Unix format, i.e., seconds since *00:00:00 UTC, January 1, 1970*.
105+
Use *mtime* = ``0`` to generate a compressed stream that does not depend on
106+
creation time. If *mtime* is omitted or ``None``, the current time is used;
107+
however, if the current time is outside the range
108+
*00:00:00 UTC, January 1, 1970* through *06:28:15 UTC, February 7, 2106*,
109+
then the value ``0`` is used instead.
107110

108111
See below for the :attr:`mtime` attribute that is set when decompressing.
109112

‎Lib/gzip.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ def _write_gzip_header(self, compresslevel):
297297
mtime = self._write_mtime
298298
if mtime is None:
299299
mtime = time.time()
300+
if mtime < 0 or mtime >= 2**32:
301+
mtime = 0
300302
write32u(self.fileobj, int(mtime))
301303
if compresslevel == _COMPRESS_LEVEL_BEST:
302304
xfl = b'\002'

‎Lib/test/test_gzip.py‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import unittest
1212
import warnings
1313
from subprocess import PIPE, Popen
14+
from unittest import mock
1415
from test.support import catch_unraisable_exception
1516
from test.support import import_helper
1617
from test.support import os_helper
@@ -351,6 +352,27 @@ def test_mtime(self):
351352
self.assertEqual(dataRead, data1)
352353
self.assertEqual(fRead.mtime, mtime)
353354

355+
def test_mtime_out_of_range(self):
356+
# exception should be raised when mtime<0 or mtime>=2**32 and is
357+
# explicitly specified
358+
with self.assertRaises(Exception):
359+
with gzip.GzipFile(self.filename, 'w', mtime=-1) as fWrite:
360+
pass
361+
with self.assertRaises(Exception):
362+
with gzip.GzipFile(self.filename, 'w', mtime=2**32) as fWrite:
363+
pass
364+
365+
# mtime should be set to 0 when time.time() is out of range and mtime is
366+
# not explicitly given
367+
for mtime in (-1, 2**32):
368+
with mock.patch('time.time', return_value=float(mtime)):
369+
with gzip.GzipFile(self.filename, 'w') as fWrite:
370+
fWrite.write(data1)
371+
with gzip.GzipFile(self.filename) as fRead:
372+
fRead.read()
373+
self.assertEqual(fRead.mtime, 0)
374+
375+
354376
def test_metadata(self):
355377
mtime = 123456789
356378

0 commit comments

Comments
 (0)