Commit 2be60afb authored by Antoine Pitrou's avatar Antoine Pitrou

Issue #11235: Fix OverflowError when trying to import a source file whose...

Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
parent 7fa5a99b
...@@ -128,6 +128,23 @@ class SimpleTest(unittest.TestCase): ...@@ -128,6 +128,23 @@ class SimpleTest(unittest.TestCase):
pycache = os.path.dirname(imp.cache_from_source(file_path)) pycache = os.path.dirname(imp.cache_from_source(file_path))
shutil.rmtree(pycache) shutil.rmtree(pycache)
def test_timestamp_overflow(self):
# When a modification timestamp is larger than 2**32, it should be
# truncated rather than raise an OverflowError.
with source_util.create_modules('_temp') as mapping:
source = mapping['_temp']
compiled = imp.cache_from_source(source)
with open(source, 'w') as f:
f.write("x = 5")
os.utime(source, (2 ** 33, 2 ** 33))
loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp'])
mod = loader.load_module('_temp')
# Sanity checks.
self.assertEqual(mod.__cached__, compiled)
self.assertEqual(mod.x, 5)
# The pyc file was created.
os.stat(compiled)
class BadBytecodeTest(unittest.TestCase): class BadBytecodeTest(unittest.TestCase):
......
...@@ -310,6 +310,18 @@ class ImportTests(unittest.TestCase): ...@@ -310,6 +310,18 @@ class ImportTests(unittest.TestCase):
""")) """))
script_helper.assert_python_ok(testfn) script_helper.assert_python_ok(testfn)
def test_timestamp_overflow(self):
# A modification timestamp larger than 2**32 should not be a problem
# when importing a module (issue #11235).
source = TESTFN + ".py"
compiled = imp.cache_from_source(source)
with open(source, 'w') as f:
pass
os.utime(source, (2 ** 33, 2 ** 33))
__import__(TESTFN)
# The pyc file was created.
os.stat(compiled)
class PycRewritingTests(unittest.TestCase): class PycRewritingTests(unittest.TestCase):
# Test that the `co_filename` attribute on code objects always points # Test that the `co_filename` attribute on code objects always points
......
...@@ -10,6 +10,9 @@ What's New in Python 3.2.3? ...@@ -10,6 +10,9 @@ What's New in Python 3.2.3?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #11235: Fix OverflowError when trying to import a source file whose
modification time doesn't fit in a 32-bit timestamp.
- Fix the builtin module initialization code to store the init function for - Fix the builtin module initialization code to store the init function for
future reinitialization. future reinitialization.
......
...@@ -1304,14 +1304,11 @@ load_source_module(char *name, char *pathname, FILE *fp) ...@@ -1304,14 +1304,11 @@ load_source_module(char *name, char *pathname, FILE *fp)
} }
#if SIZEOF_TIME_T > 4 #if SIZEOF_TIME_T > 4
/* Python's .pyc timestamp handling presumes that the timestamp fits /* Python's .pyc timestamp handling presumes that the timestamp fits
in 4 bytes. This will be fine until sometime in the year 2038, in 4 bytes. Since the code only does an equality comparison,
when a 4-byte signed time_t will overflow. ordering is not important and we can safely ignore the higher bits
(collisions are extremely unlikely).
*/ */
if (st.st_mtime >> 32) { st.st_mtime &= 0xFFFFFFFF;
PyErr_SetString(PyExc_OverflowError,
"modification time overflows a 4 byte field");
return NULL;
}
#endif #endif
cpathname = make_compiled_pathname( cpathname = make_compiled_pathname(
pathname, buf, (size_t)MAXPATHLEN + 1, !Py_OptimizeFlag); pathname, buf, (size_t)MAXPATHLEN + 1, !Py_OptimizeFlag);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment