]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-78878: Fix crash when creating an instance of `_ctypes.CField` (#14837)
authorHai Shi <shihai1992@gmail.com>
Wed, 21 Dec 2022 17:31:17 +0000 (01:31 +0800)
committerGitHub <noreply@github.com>
Wed, 21 Dec 2022 17:31:17 +0000 (23:01 +0530)
Lib/test/test_ctypes/test_struct_fields.py
Misc/NEWS.d/next/Library/2022-12-19-20-54-04.gh-issue-78878.JrkYqJ.rst [new file with mode: 0644]
Modules/_ctypes/cfield.c
Modules/_ctypes/stgdict.c

index fefeaea1496a3e8f35bf384cb35683840fe83031..e444f5e1f77919b1a0a714f4d9517d364fe66645 100644 (file)
@@ -54,6 +54,12 @@ class StructFieldsTestCase(unittest.TestCase):
         x.char = b'a\0b\0'
         self.assertEqual(bytes(x), b'a\x00###')
 
+    def test_6(self):
+        class X(Structure):
+            _fields_ = [("x", c_int)]
+        CField = type(X.x)
+        self.assertRaises(TypeError, CField)
+
     def test_gh99275(self):
         class BrokenStructure(Structure):
             def __init_subclass__(cls, **kwargs):
diff --git a/Misc/NEWS.d/next/Library/2022-12-19-20-54-04.gh-issue-78878.JrkYqJ.rst b/Misc/NEWS.d/next/Library/2022-12-19-20-54-04.gh-issue-78878.JrkYqJ.rst
new file mode 100644 (file)
index 0000000..8b455fd
--- /dev/null
@@ -0,0 +1 @@
+Fix crash when creating an instance of :class:`!_ctypes.CField`.
index 791aeba66539e9669372388216fcb7c595db468d..796a1bec966de15325ea9eca21203dbaf585f2c3 100644 (file)
@@ -30,13 +30,6 @@ static void pymem_destructor(PyObject *ptr)
 /*
   PyCField_Type
 */
-static PyObject *
-PyCField_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
-{
-    CFieldObject *obj;
-    obj = (CFieldObject *)type->tp_alloc(type, 0);
-    return (PyObject *)obj;
-}
 
 /*
  * 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,
 #define CONT_BITFIELD 2
 #define EXPAND_BITFIELD 3
 
-    self = (CFieldObject *)_PyObject_CallNoArgs((PyObject *)&PyCField_Type);
+    self = (CFieldObject *)PyCField_Type.tp_alloc((PyTypeObject *)&PyCField_Type, 0);
     if (self == NULL)
         return NULL;
     dict = PyType_stgdict(desc);
@@ -341,7 +334,7 @@ PyTypeObject PyCField_Type = {
     0,                                          /* tp_dictoffset */
     0,                                          /* tp_init */
     0,                                          /* tp_alloc */
-    PyCField_new,                               /* tp_new */
+    0,                                          /* tp_new */
     0,                                          /* tp_free */
 };
 
index 099331ca8bdb9c9e84718b6c8529572ca382d43a..9a4041fb25280ed3c53bf4a17673ce18d3c729ed 100644 (file)
@@ -257,7 +257,7 @@ MakeFields(PyObject *type, CFieldObject *descr,
             }
             continue;
         }
-        new_descr = (CFieldObject *)_PyObject_CallNoArgs((PyObject *)&PyCField_Type);
+        new_descr = (CFieldObject *)PyCField_Type.tp_alloc((PyTypeObject *)&PyCField_Type, 0);
         if (new_descr == NULL) {
             Py_DECREF(fdescr);
             Py_DECREF(fieldlist);