]> granicus.if.org Git - python/commitdiff
bpo-31577: Fix a crash in os.utime() in case of a bad ns argument. (GH-3752)
authorOren Milman <orenmn@gmail.com>
Wed, 12 Sep 2018 19:14:35 +0000 (22:14 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 12 Sep 2018 19:14:35 +0000 (22:14 +0300)
Lib/test/test_os.py
Misc/NEWS.d/next/Core and Builtins/2017-09-25-20-36-24.bpo-31577.jgYsSA.rst [new file with mode: 0644]
Modules/posixmodule.c

index 6658a61ea2a74a53430024bf756694cce5ebc489..0a8ba5cf95f12bbd6be4509002fbfb46c46db6a0 100644 (file)
@@ -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 (file)
index 0000000..8142882
--- /dev/null
@@ -0,0 +1,2 @@
+Fix a crash in `os.utime()` in case of a bad ns argument. Patch by Oren
+Milman.
index 48bd5dc3941557846018e635a01f53bc44bc8573..7c02351a466130befde92bd10774600a9b0efe0d 100644 (file)
@@ -4630,6 +4630,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;