Skip to content

gh-127870: Fix ctypes _as_parameter_ handling #127872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2024
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: 10 additions & 2 deletions Lib/test/test_ctypes/test_as_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,16 @@ class A:

a = A()
a._as_parameter_ = a
with self.assertRaises(RecursionError):
c_int.from_param(a)
for c_type in (
ctypes.c_wchar_p,
ctypes.c_char_p,
ctypes.c_void_p,
ctypes.c_int, # PyCSimpleType
POINT, # CDataType
):
with self.subTest(c_type=c_type):
with self.assertRaises(RecursionError):
c_type.from_param(a)


class AsParamWrapper:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Detect recursive calls in ctypes ``_as_parameter_`` handling.
Patch by Victor Stinner.
22 changes: 21 additions & 1 deletion Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -1052,8 +1052,13 @@ CDataType_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value)
return NULL;
}
if (as_parameter) {
if (_Py_EnterRecursiveCall(" while processing _as_parameter_")) {
Py_DECREF(as_parameter);
return NULL;
}
value = CDataType_from_param_impl(type, cls, as_parameter);
Py_DECREF(as_parameter);
_Py_LeaveRecursiveCall();
return value;
}
PyErr_Format(PyExc_TypeError,
Expand Down Expand Up @@ -1843,8 +1848,13 @@ c_wchar_p_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value)
return NULL;
}
if (as_parameter) {
if (_Py_EnterRecursiveCall(" while processing _as_parameter_")) {
Py_DECREF(as_parameter);
return NULL;
}
value = c_wchar_p_from_param_impl(type, cls, as_parameter);
Py_DECREF(as_parameter);
_Py_LeaveRecursiveCall();
return value;
}
PyErr_Format(PyExc_TypeError,
Expand Down Expand Up @@ -1927,8 +1937,13 @@ c_char_p_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value)
return NULL;
}
if (as_parameter) {
if (_Py_EnterRecursiveCall(" while processing _as_parameter_")) {
Py_DECREF(as_parameter);
return NULL;
}
value = c_char_p_from_param_impl(type, cls, as_parameter);
Py_DECREF(as_parameter);
_Py_LeaveRecursiveCall();
return value;
}
PyErr_Format(PyExc_TypeError,
Expand Down Expand Up @@ -2079,8 +2094,13 @@ c_void_p_from_param_impl(PyObject *type, PyTypeObject *cls, PyObject *value)
return NULL;
}
if (as_parameter) {
if (_Py_EnterRecursiveCall(" while processing _as_parameter_")) {
Py_DECREF(as_parameter);
return NULL;
}
value = c_void_p_from_param_impl(type, cls, as_parameter);
Py_DECREF(as_parameter);
_Py_LeaveRecursiveCall();
return value;
}
PyErr_Format(PyExc_TypeError,
Expand Down Expand Up @@ -2447,9 +2467,9 @@ PyCSimpleType_from_param_impl(PyObject *type, PyTypeObject *cls,
return NULL;
}
value = PyCSimpleType_from_param_impl(type, cls, as_parameter);
_Py_LeaveRecursiveCall();
Py_DECREF(as_parameter);
Py_XDECREF(exc);
_Py_LeaveRecursiveCall();
return value;
}
if (exc) {
Expand Down
Loading