Skip to content

Commit 5b06d2c

Browse files
gh-127945: add lock held assertions in ctypes arrays (#132720)
1 parent 69cda31 commit 5b06d2c

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

Modules/_ctypes/_ctypes.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3356,14 +3356,13 @@ _PyCData_set(ctypes_state *st,
33563356
CDataObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
33573357
Py_ssize_t size, char *ptr)
33583358
{
3359+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(dst);
33593360
CDataObject *src;
33603361
int err;
33613362

33623363
if (setfunc) {
33633364
PyObject *res;
3364-
Py_BEGIN_CRITICAL_SECTION(dst);
33653365
res = setfunc(ptr, value, size);
3366-
Py_END_CRITICAL_SECTION();
33673366
return res;
33683367
}
33693368
if (!CDataObject_Check(st, value)) {
@@ -3373,9 +3372,7 @@ _PyCData_set(ctypes_state *st,
33733372
}
33743373
if (info && info->setfunc) {
33753374
PyObject *res;
3376-
Py_BEGIN_CRITICAL_SECTION(dst);
33773375
res = info->setfunc(ptr, value, size);
3378-
Py_END_CRITICAL_SECTION();
33793376
return res;
33803377
}
33813378
/*
@@ -3397,9 +3394,7 @@ _PyCData_set(ctypes_state *st,
33973394
Py_DECREF(ob);
33983395
return result;
33993396
} else if (value == Py_None && PyCPointerTypeObject_Check(st, type)) {
3400-
Py_BEGIN_CRITICAL_SECTION(dst);
34013397
*(void **)ptr = NULL;
3402-
Py_END_CRITICAL_SECTION();
34033398
Py_RETURN_NONE;
34043399
} else {
34053400
PyErr_Format(PyExc_TypeError,
@@ -3417,11 +3412,6 @@ _PyCData_set(ctypes_state *st,
34173412
if (err) {
34183413
Py_BEGIN_CRITICAL_SECTION(src);
34193414
memcpy(ptr, src->b_ptr, size);
3420-
3421-
if (PyCPointerTypeObject_Check(st, type)) {
3422-
/* XXX */
3423-
}
3424-
34253415
value = GetKeepedObjects(src);
34263416
Py_END_CRITICAL_SECTION();
34273417
if (value == NULL)
@@ -3485,6 +3475,8 @@ PyCData_set(ctypes_state *st,
34853475
PyObject *dst, PyObject *type, SETFUNC setfunc, PyObject *value,
34863476
Py_ssize_t index, Py_ssize_t size, char *ptr)
34873477
{
3478+
_Py_CRITICAL_SECTION_ASSERT_OBJECT_LOCKED(dst);
3479+
34883480
CDataObject *mem = (CDataObject *)dst;
34893481
PyObject *result;
34903482

Modules/_ctypes/cfield.c

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ _pack_legacy_size(CFieldObject *field)
243243
}
244244

245245
static int
246-
PyCField_set(PyObject *op, PyObject *inst, PyObject *value)
246+
PyCField_set_lock_held(PyObject *op, PyObject *inst, PyObject *value)
247247
{
248248
CDataObject *dst;
249249
char *ptr;
@@ -265,6 +265,16 @@ PyCField_set(PyObject *op, PyObject *inst, PyObject *value)
265265
self->index, _pack_legacy_size(self), ptr);
266266
}
267267

268+
static int
269+
PyCField_set(PyObject *op, PyObject *inst, PyObject *value)
270+
{
271+
int res;
272+
Py_BEGIN_CRITICAL_SECTION(inst);
273+
res = PyCField_set_lock_held(op, inst, value);
274+
Py_END_CRITICAL_SECTION();
275+
return res;
276+
}
277+
268278
static PyObject *
269279
PyCField_get(PyObject *op, PyObject *inst, PyObject *type)
270280
{
@@ -280,9 +290,13 @@ PyCField_get(PyObject *op, PyObject *inst, PyObject *type)
280290
return NULL;
281291
}
282292
src = _CDataObject_CAST(inst);
283-
return PyCData_get(st, self->proto, self->getfunc, inst,
293+
PyObject *res;
294+
Py_BEGIN_CRITICAL_SECTION(inst);
295+
res = PyCData_get(st, self->proto, self->getfunc, inst,
284296
self->index, _pack_legacy_size(self),
285297
src->b_ptr + self->byte_offset);
298+
Py_END_CRITICAL_SECTION();
299+
return res;
286300
}
287301

288302
static PyObject *

0 commit comments

Comments
 (0)