From c4113a520f97bbd07a6c4cddf5bcae55bddde9ee Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Mon, 25 Sep 2017 20:52:36 +0300 Subject: [PATCH 1/2] init commit --- Lib/test/test_os.py | 15 +++++++++++++++ .../2017-09-25-20-36-24.bpo-31577.jgYsSA.rst | 2 ++ Modules/posixmodule.c | 6 ++++++ 3 files changed, 23 insertions(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2017-09-25-20-36-24.bpo-31577.jgYsSA.rst diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index d7d08cece3cd043..d9429c91a86c51d 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -672,6 +672,21 @@ def test_utime_invalid_arguments(self): with self.assertRaises(ValueError): os.utime(self.fname, (5, 5), ns=(5, 5)) + 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 000000000000000..81428828af2a6a3 --- /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 83135e536a51b23..8cf5660008d677b 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -4587,6 +4587,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; From 457bd1706d814bb05279502597ada1b179f89d27 Mon Sep 17 00:00:00 2001 From: Oren Milman Date: Mon, 25 Sep 2017 22:23:59 +0300 Subject: [PATCH 2/2] add @cpython_only --- Lib/test/test_os.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py index d9429c91a86c51d..f61a1431bac8d58 100644 --- a/Lib/test/test_os.py +++ b/Lib/test/test_os.py @@ -672,6 +672,7 @@ def test_utime_invalid_arguments(self): 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.