diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2018-09-12 12:46:30 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-09-12 12:46:30 -0700 |
commit | 329ea4ef7cc3a907a64c6f0702fc93206b6744de (patch) | |
tree | 300bb0c3aa68ad260538ffe98528f69eefba2857 | |
parent | 72c34cf6dd5fb206fe4d407ff603468af466fd2e (diff) | |
download | cpython-git-329ea4ef7cc3a907a64c6f0702fc93206b6744de.tar.gz |
bpo-31577: Fix a crash in os.utime() in case of a bad ns argument. (GH-3752)
(cherry picked from commit 0bd1a2dcfdf36b181385ae61361e7692f4ebb0fd)
Co-authored-by: Oren Milman <orenmn@gmail.com>
-rw-r--r-- | Lib/test/test_os.py | 16 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Core and Builtins/2017-09-25-20-36-24.bpo-31577.jgYsSA.rst | 2 | ||||
-rw-r--r-- | Modules/posixmodule.c | 6 |
3 files changed, 24 insertions, 0 deletions
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index a140ae0d29..79ddc48eeb 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -635,6 +635,22 @@ class UtimeTests(unittest.TestCase): with self.assertRaises(ValueError): os.utime(self.fname, (5, 5), ns=(5, 5)) + @support.cpython_only + def test_issue31577(self): + # The interpreter shouldn't crash in case utime() received a bad + # ns argument. + def get_bad_int(divmod_ret_val): + class BadInt: + def __divmod__(*args): + return divmod_ret_val + return BadInt() + with self.assertRaises(TypeError): + os.utime(self.fname, ns=(get_bad_int(42), 1)) + with self.assertRaises(TypeError): + os.utime(self.fname, ns=(get_bad_int(()), 1)) + with self.assertRaises(TypeError): + os.utime(self.fname, ns=(get_bad_int((1, 2, 3)), 1)) + from test import mapping_tests diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-09-25-20-36-24.bpo-31577.jgYsSA.rst b/Misc/NEWS.d/next/Core and Builtins/2017-09-25-20-36-24.bpo-31577.jgYsSA.rst new file mode 100644 index 0000000000..81428828af --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2017-09-25-20-36-24.bpo-31577.jgYsSA.rst @@ -0,0 +1,2 @@ +Fix a crash in `os.utime()` in case of a bad ns argument. Patch by Oren +Milman. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index d487e5386e..bbbff1eca8 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -4563,6 +4563,12 @@ split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns) divmod = PyNumber_Divmod(py_long, billion); if (!divmod) goto exit; + if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) { + PyErr_Format(PyExc_TypeError, + "%.200s.__divmod__() must return a 2-tuple, not %.200s", + Py_TYPE(py_long)->tp_name, Py_TYPE(divmod)->tp_name); + goto exit; + } *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0)); if ((*s == -1) && PyErr_Occurred()) goto exit; |