[Python-checkins] cpython (3.5): Issue #26709: Fixed Y2038 problem in loading binary PLists.

serhiy.storchaka python-checkins at python.org
Fri Apr 8 08:00:57 EDT 2016


https://hg.python.org/cpython/rev/ba35b0404163
changeset:   100870:ba35b0404163
branch:      3.5
parent:      100866:197e1f8b28b7
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Fri Apr 08 15:00:02 2016 +0300
summary:
  Issue #26709: Fixed Y2038 problem in loading binary PLists.

files:
  Lib/plistlib.py           |  2 +-
  Lib/test/test_plistlib.py |  9 +++++++++
  Misc/NEWS                 |  2 ++
  3 files changed, 12 insertions(+), 1 deletions(-)


diff --git a/Lib/plistlib.py b/Lib/plistlib.py
--- a/Lib/plistlib.py
+++ b/Lib/plistlib.py
@@ -685,7 +685,7 @@
             f = struct.unpack('>d', self._fp.read(8))[0]
             # timestamp 0 of binary plists corresponds to 1/1/2001
             # (year of Mac OS X 10.0), instead of 1/1/1970.
-            return datetime.datetime.utcfromtimestamp(f + (31 * 365 + 8) * 86400)
+            return datetime.datetime(2001, 1, 1) + datetime.timedelta(seconds=f)
 
         elif tokenH == 0x40:  # data
             s = self._get_size(tokenL)
diff --git a/Lib/test/test_plistlib.py b/Lib/test/test_plistlib.py
--- a/Lib/test/test_plistlib.py
+++ b/Lib/test/test_plistlib.py
@@ -428,6 +428,15 @@
                 b'\x00\x00\x00\x00\x00\x00\x00\x13')
         self.assertEqual(plistlib.loads(data), {'a': 'b'})
 
+    def test_large_timestamp(self):
+        # Issue #26709: 32-bit timestamp out of range
+        for ts in -2**31-1, 2**31:
+            with self.subTest(ts=ts):
+                d = (datetime.datetime.utcfromtimestamp(0) +
+                     datetime.timedelta(seconds=ts))
+                data = plistlib.dumps(d, fmt=plistlib.FMT_BINARY)
+                self.assertEqual(plistlib.loads(data), d)
+
 
 class TestPlistlibDeprecated(unittest.TestCase):
     def test_io_deprecated(self):
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -99,6 +99,8 @@
 Library
 -------
 
+- Issue #26709: Fixed Y2038 problem in loading binary PLists.
+
 - Issue #23735: Handle terminal resizing with Readline 6.3+ by installing our
   own SIGWINCH handler.  Patch by Eric Price.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list