Skip to content

Commit d713c54

Browse files
authored
gh-78878: Fix crash when creating an instance of _ctypes.CField (#14837)
1 parent e0b4d96 commit d713c54

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

Lib/test/test_ctypes/test_struct_fields.py

+6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ class X(Structure):
5454
x.char = b'a\0b\0'
5555
self.assertEqual(bytes(x), b'a\x00###')
5656

57+
def test_6(self):
58+
class X(Structure):
59+
_fields_ = [("x", c_int)]
60+
CField = type(X.x)
61+
self.assertRaises(TypeError, CField)
62+
5763
def test_gh99275(self):
5864
class BrokenStructure(Structure):
5965
def __init_subclass__(cls, **kwargs):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix crash when creating an instance of :class:`!_ctypes.CField`.

Modules/_ctypes/cfield.c

+2-9
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ static void pymem_destructor(PyObject *ptr)
3030
/*
3131
PyCField_Type
3232
*/
33-
static PyObject *
34-
PyCField_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
35-
{
36-
CFieldObject *obj;
37-
obj = (CFieldObject *)type->tp_alloc(type, 0);
38-
return (PyObject *)obj;
39-
}
4033

4134
/*
4235
* Expects the size, index and offset for the current field in *psize and
@@ -68,7 +61,7 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
6861
#define CONT_BITFIELD 2
6962
#define EXPAND_BITFIELD 3
7063

71-
self = (CFieldObject *)_PyObject_CallNoArgs((PyObject *)&PyCField_Type);
64+
self = (CFieldObject *)PyCField_Type.tp_alloc((PyTypeObject *)&PyCField_Type, 0);
7265
if (self == NULL)
7366
return NULL;
7467
dict = PyType_stgdict(desc);
@@ -341,7 +334,7 @@ PyTypeObject PyCField_Type = {
341334
0, /* tp_dictoffset */
342335
0, /* tp_init */
343336
0, /* tp_alloc */
344-
PyCField_new, /* tp_new */
337+
0, /* tp_new */
345338
0, /* tp_free */
346339
};
347340

Modules/_ctypes/stgdict.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ MakeFields(PyObject *type, CFieldObject *descr,
257257
}
258258
continue;
259259
}
260-
new_descr = (CFieldObject *)_PyObject_CallNoArgs((PyObject *)&PyCField_Type);
260+
new_descr = (CFieldObject *)PyCField_Type.tp_alloc((PyTypeObject *)&PyCField_Type, 0);
261261
if (new_descr == NULL) {
262262
Py_DECREF(fdescr);
263263
Py_DECREF(fieldlist);

0 commit comments

Comments
 (0)