-
-
Notifications
You must be signed in to change notification settings - Fork 33k
gh-138008: Fix segfaults in _ctypes due to invalid argtypes #138285
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
Changes from 3 commits
0332605
07c9af5
46037b1
45f483c
9c9a65e
40a1dce
41f01a6
adf387f
71f5eda
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix segmentation faults in the :mod:`ctypes` module due to invalid :attr:`argtypes`. Patch by Dung Nguyen. | ||
magnified103 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3647,6 +3647,9 @@ atomic_xgetref(PyObject *obj, PyObject **field) | |
#endif | ||
} | ||
|
||
static int | ||
_validate_paramflags(ctypes_state *st, PyTypeObject *type, PyObject *paramflags, PyObject *argtypes); | ||
|
||
|
||
|
||
/*[clinic input] | ||
|
@@ -3762,14 +3765,27 @@ _ctypes_CFuncPtr_argtypes_set_impl(PyCFuncPtrObject *self, PyObject *value) | |
{ | ||
PyObject *converters; | ||
|
||
PyTypeObject *type = Py_TYPE(self); | ||
ctypes_state *st = get_module_state_by_def(Py_TYPE(type)); | ||
|
||
if (value == NULL || value == Py_None) { | ||
/* Verify paramflags again due to constraints with argtypes */ | ||
if (!_validate_paramflags(st, type, self->paramflags, value)) { | ||
magnified103 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return -1; | ||
} | ||
|
||
atomic_xsetref(&self->argtypes, NULL); | ||
atomic_xsetref(&self->converters, NULL); | ||
} else { | ||
ctypes_state *st = get_module_state_by_def(Py_TYPE(Py_TYPE(self))); | ||
converters = converters_from_argtypes(st, value); | ||
if (!converters) | ||
return -1; | ||
|
||
/* Verify paramflags again due to constraints with argtypes */ | ||
if (!_validate_paramflags(st, type, self->paramflags, value)) { | ||
Py_DECREF(converters); | ||
return -1; | ||
} | ||
atomic_xsetref(&self->converters, converters); | ||
Py_INCREF(value); | ||
atomic_xsetref(&self->argtypes, value); | ||
|
@@ -3899,10 +3915,9 @@ _check_outarg_type(ctypes_state *st, PyObject *arg, Py_ssize_t index) | |
|
||
/* Returns 1 on success, 0 on error */ | ||
static int | ||
_validate_paramflags(ctypes_state *st, PyTypeObject *type, PyObject *paramflags) | ||
_validate_paramflags(ctypes_state *st, PyTypeObject *type, PyObject *paramflags, PyObject *argtypes) | ||
{ | ||
Py_ssize_t i, len; | ||
PyObject *argtypes; | ||
|
||
StgInfo *info; | ||
if (PyStgInfo_FromType(st, (PyObject *)type, &info) < 0) { | ||
|
@@ -3913,9 +3928,11 @@ _validate_paramflags(ctypes_state *st, PyTypeObject *type, PyObject *paramflags) | |
"abstract class"); | ||
return 0; | ||
} | ||
argtypes = info->argtypes; | ||
if (argtypes == NULL || argtypes == Py_None) { | ||
|
||
argtypes = info->argtypes; | ||
} | ||
|
||
if (paramflags == NULL || info->argtypes == NULL) | ||
if (paramflags == NULL || argtypes == NULL) | ||
return 1; | ||
magnified103 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
if (!PyTuple_Check(paramflags)) { | ||
|
@@ -3925,7 +3942,7 @@ _validate_paramflags(ctypes_state *st, PyTypeObject *type, PyObject *paramflags) | |
} | ||
|
||
len = PyTuple_GET_SIZE(paramflags); | ||
if (len != PyTuple_GET_SIZE(info->argtypes)) { | ||
if (len != PyTuple_GET_SIZE(argtypes)) { | ||
PyErr_SetString(PyExc_ValueError, | ||
"paramflags must have the same length as argtypes"); | ||
return 0; | ||
|
@@ -4101,7 +4118,7 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
#endif | ||
#undef USE_DLERROR | ||
ctypes_state *st = get_module_state_by_def(Py_TYPE(type)); | ||
if (!_validate_paramflags(st, type, paramflags)) { | ||
if (!_validate_paramflags(st, type, paramflags, NULL)) { | ||
Py_DECREF(ftuple); | ||
return NULL; | ||
} | ||
|
@@ -4145,7 +4162,7 @@ PyCFuncPtr_FromVtblIndex(PyTypeObject *type, PyObject *args, PyObject *kwds) | |
paramflags = NULL; | ||
|
||
ctypes_state *st = get_module_state_by_def(Py_TYPE(type)); | ||
if (!_validate_paramflags(st, type, paramflags)) { | ||
if (!_validate_paramflags(st, type, paramflags, NULL)) { | ||
return NULL; | ||
} | ||
self = (PyCFuncPtrObject *)generic_pycdata_new(st, type, args, kwds); | ||
|
Uh oh!
There was an error while loading. Please reload this page.