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
12 changes: 12 additions & 0 deletions Lib/test/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,18 @@ def test_pylong_int_divmod(self):
a, b = divmod(n*3 + 1, n)
assert a == 3 and b == 1

@support.cpython_only # tests implementation details of CPython.
@unittest.skipUnless(_pylong, "_pylong module required")
def test_pylong_int_divmod_crash(self):
# Regression test for https://github.com/python/cpython/issues/142554.
bad_int_divmod = lambda a, b: (1,)
# 'k' chosen such that divmod(2**(2*k), 2**k) uses _pylong.int_divmod()
k = 10_000
a, b = (1 << (2 * k)), (1 << k)
with mock.patch.object(_pylong, "int_divmod", wraps=bad_int_divmod):
msg = r"tuple of length 2 is required from int_divmod\(\)"
self.assertRaisesRegex(ValueError, msg, divmod, a, b)

def test_pylong_str_to_int(self):
v1 = 1 << 100_000
s = str(v1)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in :func:`divmod` when :func:`!_pylong.int_divmod` does not
return a tuple of length two exactly. Patch by Bénédikt Tran.
4 changes: 2 additions & 2 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -4339,10 +4339,10 @@ pylong_int_divmod(PyLongObject *v, PyLongObject *w,
if (result == NULL) {
return -1;
}
if (!PyTuple_Check(result)) {
if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 2) {
Py_DECREF(result);
PyErr_SetString(PyExc_ValueError,
"tuple is required from int_divmod()");
"tuple of length 2 is required from int_divmod()");
return -1;
}
PyObject *q = PyTuple_GET_ITEM(result, 0);
Expand Down
Loading