Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,22 @@ 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.
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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in `os.utime()` in case of a bad ns argument. Patch by Oren
Milman.
6 changes: 6 additions & 0 deletions Modules/posixmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down