Skip to content

gh-127945: add lock held assertions in ctypes arrays #132720

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
Apr 19, 2025
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
14 changes: 3 additions & 11 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -3356,14 +3356,13 @@ _PyCData_set(ctypes_state *st,
CDataObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
Py_ssize_t size, char *ptr)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(dst);
CDataObject *src;
int err;

if (setfunc) {
PyObject *res;
Py_BEGIN_CRITICAL_SECTION(dst);
res = setfunc(ptr, value, size);
Py_END_CRITICAL_SECTION();
return res;
}
if (!CDataObject_Check(st, value)) {
Expand All @@ -3373,9 +3372,7 @@ _PyCData_set(ctypes_state *st,
}
if (info && info->setfunc) {
PyObject *res;
Py_BEGIN_CRITICAL_SECTION(dst);
res = info->setfunc(ptr, value, size);
Py_END_CRITICAL_SECTION();
return res;
}
/*
Expand All @@ -3397,9 +3394,7 @@ _PyCData_set(ctypes_state *st,
Py_DECREF(ob);
return result;
} else if (value == Py_None && PyCPointerTypeObject_Check(st, type)) {
Py_BEGIN_CRITICAL_SECTION(dst);
*(void **)ptr = NULL;
Py_END_CRITICAL_SECTION();
Py_RETURN_NONE;
} else {
PyErr_Format(PyExc_TypeError,
Expand All @@ -3417,11 +3412,6 @@ _PyCData_set(ctypes_state *st,
if (err) {
Py_BEGIN_CRITICAL_SECTION(src);
memcpy(ptr, src->b_ptr, size);

if (PyCPointerTypeObject_Check(st, type)) {
/* XXX */
}

value = GetKeepedObjects(src);
Py_END_CRITICAL_SECTION();
if (value == NULL)
Expand Down Expand Up @@ -3485,6 +3475,8 @@ PyCData_set(ctypes_state *st,
PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
Py_ssize_t index, Py_ssize_t size, char *ptr)
{
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(dst);

CDataObject *mem = (CDataObject *)dst;
PyObject *result;

Expand Down
18 changes: 16 additions & 2 deletions Modules/_ctypes/cfield.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ _pack_legacy_size(CFieldObject *field)
}

static int
PyCField_set(PyObject *op, PyObject *inst, PyObject *value)
PyCField_set_lock_held(PyObject *op, PyObject *inst, PyObject *value)
{
CDataObject *dst;
char *ptr;
Expand All @@ -265,6 +265,16 @@ PyCField_set(PyObject *op, PyObject *inst, PyObject *value)
self->index, _pack_legacy_size(self), ptr);
}

static int
PyCField_set(PyObject *op, PyObject *inst, PyObject *value)
{
int res;
Py_BEGIN_CRITICAL_SECTION(inst);
res = PyCField_set_lock_held(op, inst, value);
Py_END_CRITICAL_SECTION();
return res;
}

static PyObject *
PyCField_get(PyObject *op, PyObject *inst, PyObject *type)
{
Expand All @@ -280,9 +290,13 @@ PyCField_get(PyObject *op, PyObject *inst, PyObject *type)
return NULL;
}
src = _CDataObject_CAST(inst);
return PyCData_get(st, self->proto, self->getfunc, inst,
PyObject *res;
Py_BEGIN_CRITICAL_SECTION(inst);
res = PyCData_get(st, self->proto, self->getfunc, inst,
self->index, _pack_legacy_size(self),
src->b_ptr + self->byte_offset);
Py_END_CRITICAL_SECTION();
return res;
}

static PyObject *
Expand Down
Loading