Skip to content

Commit

Permalink
pythongh-127870: Detect recursive calls in ctypes _as_parameter_ hand…
Browse files Browse the repository at this point in the history
…ling (python#127872)

(cherry picked from commit 6ff38fc)
  • Loading branch information
vstinner committed Dec 13, 2024
1 parent 6544f99 commit f1f525c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
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 @@ -199,8 +199,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)


#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 @@ -847,8 +847,13 @@ CDataType_from_param(PyObject *type, PyObject *value)
return NULL;
}
if (as_parameter) {
if (_Py_EnterRecursiveCall(" while processing _as_parameter_")) {
Py_DECREF(as_parameter);
return NULL;
}
value = CDataType_from_param(type, as_parameter);
Py_DECREF(as_parameter);
_Py_LeaveRecursiveCall();
return value;
}
PyErr_Format(PyExc_TypeError,
Expand Down Expand Up @@ -1716,8 +1721,13 @@ c_wchar_p_from_param(PyObject *type, 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(type, as_parameter);
Py_DECREF(as_parameter);
_Py_LeaveRecursiveCall();
return value;
}
/* XXX better message */
Expand Down Expand Up @@ -1780,8 +1790,13 @@ c_char_p_from_param(PyObject *type, 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(type, as_parameter);
Py_DECREF(as_parameter);
_Py_LeaveRecursiveCall();
return value;
}
/* XXX better message */
Expand Down Expand Up @@ -1915,8 +1930,13 @@ c_void_p_from_param(PyObject *type, 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(type, as_parameter);
Py_DECREF(as_parameter);
_Py_LeaveRecursiveCall();
return value;
}
/* XXX better message */
Expand Down Expand Up @@ -2275,9 +2295,9 @@ PyCSimpleType_from_param(PyObject *type, PyObject *value)
return NULL;
}
value = PyCSimpleType_from_param(type, as_parameter);
_Py_LeaveRecursiveCall();
Py_DECREF(as_parameter);
Py_XDECREF(exc);
_Py_LeaveRecursiveCall();
return value;
}
if (exc) {
Expand Down

0 comments on commit f1f525c

Please sign in to comment.