Title: gzip: cannot create file if mtime > `2106-02-07T06:28:15` · Issue #133998 · python/cpython · GitHub
Open Graph Title: gzip: cannot create file if mtime > `2106-02-07T06:28:15` · Issue #133998 · python/cpython
X Title: gzip: cannot create file if mtime > `2106-02-07T06:28:15` · Issue #133998 · python/cpython
Description: Bug report Bug description: We had a VM where ntp went very wonky and ended up thinking it was the year 2141! Trying to write a gzip file with the system clock greater than 2106-02-07T06:28:15 results in a struct error because we tried t...
Open Graph Description: Bug report Bug description: We had a VM where ntp went very wonky and ended up thinking it was the year 2141! Trying to write a gzip file with the system clock greater than 2106-02-07T06:28:15 resu...
X Description: Bug report Bug description: We had a VM where ntp went very wonky and ended up thinking it was the year 2141! Trying to write a gzip file with the system clock greater than 2106-02-07T06:28:15 resu...
Opengraph URL: https://github.com/python/cpython/issues/133998
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"gzip: cannot create file if mtime \u003e `2106-02-07T06:28:15`","articleBody":"# Bug report\n\n### Bug description:\n\nWe had a VM where ntp went very wonky and ended up thinking it was the year 2141!\nTrying to write a gzip file with the system clock greater than `2106-02-07T06:28:15` results in a struct error because we tried to cram a 64-bit int into a 32 bit field (which won't work).\n\nI wouldn't expect the gzip module to explode in this case, I'd expect it to set the `MTIME` to 0.\n[RFC 1952](https://datatracker.ietf.org/doc/html/rfc1952#page-7) (if that's at all relevant these days) states\n\u003e MTIME = 0 means no time stamp is available\n\n### MWE\nOnly tested this on macOS on 3.13.2, but from a cursory glance, the mtime handling hasn't changed in over a decade.\n```python\n\u003e\u003e\u003e import gzip\n\u003e\u003e\u003e gzip.GzipFile(\"/dev/null\", \"w\", mtime=2**32+1)\nTraceback (most recent call last):\n File \"\u003cpython-input-3\u003e\", line 1, in \u003cmodule\u003e\n gzip.GzipFile(\"/dev/null\", \"w\", mtime=2**32+1)\n ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.13/gzip.py\", line 237, in __init__\n self._write_gzip_header(compresslevel)\n ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.13/gzip.py\", line 281, in _write_gzip_header\n write32u(self.fileobj, int(mtime))\n ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^\n File \"/usr/local/lib/python3.13/gzip.py\", line 77, in write32u\n output.write(struct.pack(\"\u003cL\", value))\n ~~~~~~~~~~~^^^^^^^^^^^^^\nstruct.error: 'L' format requires 0 \u003c= number \u003c= 4294967295\n```\n\n### Proposed Patch\nI'd naïvely fix it with this patch\n```diff\ndiff --git a/Lib/gzip.py b/Lib/gzip.py\nindex c00f51858de..29345de4659 100644\n--- a/Lib/gzip.py\n+++ b/Lib/gzip.py\n@@ -297,6 +297,8 @@ def _write_gzip_header(self, compresslevel):\n mtime = self._write_mtime\n if mtime is None:\n mtime = time.time()\n+ if mtime \u003e 4294967295:\n+ mtime = 0\n write32u(self.fileobj, int(mtime))\n if compresslevel == _COMPRESS_LEVEL_BEST:\n xfl = b'\\002'\n```\n\n\n### CPython versions tested on:\n\n3.13\n\n### Operating systems tested on:\n\nmacOS\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-134278\n* gh-150221\n* gh-151828\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/huwcbjones","@type":"Person","name":"huwcbjones"},"datePublished":"2025-05-14T11:06:07.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/133998/cpython/issues/133998"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:a15be44c-61ca-1750-2aad-189b822f5194 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B7A0:328753:3E33DF:52EF1A:6A504362 |
| html-safe-nonce | 1de6c33ff8af2c3ba8b66d1cc8f2b50bb3eacfccf652fbe47d01b42c5162a240 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCN0EwOjMyODc1MzozRTMzREY6NTJFRjFBOjZBNTA0MzYyIiwidmlzaXRvcl9pZCI6Ijg5NjkzMjQ1NjQxNjYyMzA4ODMiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 231e85b01127565ce658531f6f0831d7665130641204052c58834105141d2fd9 |
| hovercard-subject-tag | issue:3062728735 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/133998/issue_layout |
| twitter:image | https://opengraph.githubassets.com/cac3c79128c66694b60b242f4ab23e56805a3464975691e816393a73acd1c71c/python/cpython/issues/133998 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/cac3c79128c66694b60b242f4ab23e56805a3464975691e816393a73acd1c71c/python/cpython/issues/133998 |
| og:image:alt | Bug report Bug description: We had a VM where ntp went very wonky and ended up thinking it was the year 2141! Trying to write a gzip file with the system clock greater than 2106-02-07T06:28:15 resu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | huwcbjones |
| hostname | github.com |
| expected-hostname | github.com |
| None | d6dc8294eb500fa36bbc57472d61fe87c522f9c3c1d64f70f4926f66a66a7efb |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d90666eaf032b7eb8975a378efe38e37f4ebb96c |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width