[Python-checkins] gh-78878: Fix crash when creating an instance of `_ctypes.CField` (GH-14837)

miss-islington webhook-mailer at python.org
Wed Dec 21 13:02:12 EST 2022


https://github.com/python/cpython/commit/3e715e0cc811ebccce4234c3e25ef30787151d00
commit: 3e715e0cc811ebccce4234c3e25ef30787151d00
branch: 3.11
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022年12月21日T10:02:05-08:00
summary:
gh-78878: Fix crash when creating an instance of `_ctypes.CField` (GH-14837)
(cherry picked from commit d713c54ac8a2eba0616a5a07714696d935f1062e)
Co-authored-by: Hai Shi <shihai1992 at gmail.com>
files:
A Misc/NEWS.d/next/Library/2022-12-19-20-54-04.gh-issue-78878.JrkYqJ.rst
M Lib/ctypes/test/test_struct_fields.py
M Modules/_ctypes/cfield.c
M Modules/_ctypes/stgdict.c
diff --git a/Lib/ctypes/test/test_struct_fields.py b/Lib/ctypes/test/test_struct_fields.py
index fefeaea1496a..e444f5e1f779 100644
--- a/Lib/ctypes/test/test_struct_fields.py
+++ b/Lib/ctypes/test/test_struct_fields.py
@@ -54,6 +54,12 @@ class X(Structure):
 x.char = b'a0円b0円'
 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
index 000000000000..8b455fd2ef7f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-12-19-20-54-04.gh-issue-78878.JrkYqJ.rst
@@ -0,0 +1 @@
+Fix crash when creating an instance of :class:`!_ctypes.CField`.
diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c
index 13ed8b7eda65..9bdf1db856e7 100644
--- a/Modules/_ctypes/cfield.c
+++ b/Modules/_ctypes/cfield.c
@@ -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);
@@ -343,7 +336,7 @@ PyTypeObject PyCField_Type = {
 0, /* tp_dictoffset */
 0, /* tp_init */
 0, /* tp_alloc */
- PyCField_new, /* tp_new */
+ 0, /* tp_new */
 0, /* tp_free */
 };
 
diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c
index 88999b8dff18..b7134ce2571b 100644
--- a/Modules/_ctypes/stgdict.c
+++ b/Modules/_ctypes/stgdict.c
@@ -258,7 +258,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);


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /