bpo-21983: Fix a crash in ctypes.cast() when passed a ctypes structured data type (GH-3859)

(cherry picked from commit d518d8bc8d)

Co-authored-by: Oren Milman <orenmn@gmail.com>
This commit is contained in:
Miss Islington (bot) 2018-05-26 11:39:52 -07:00 committed by GitHub
parent d49862582e
commit e60f6e1864
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 1 deletions

View File

@ -82,5 +82,18 @@ class Test(unittest.TestCase):
self.assertEqual(cast(cast(s, c_void_p), c_wchar_p).value,
"hiho")
def test_bad_type_arg(self):
# The type argument must be a ctypes pointer type.
array_type = c_byte * sizeof(c_int)
array = array_type()
self.assertRaises(TypeError, cast, array, None)
self.assertRaises(TypeError, cast, array, array_type)
class Struct(Structure):
_fields_ = [("a", c_int)]
self.assertRaises(TypeError, cast, array, Struct)
class MyUnion(Union):
_fields_ = [("a", c_int)]
self.assertRaises(TypeError, cast, array, MyUnion)
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,2 @@
Fix a crash in `ctypes.cast()` in case the type argument is a ctypes
structured data type. Patch by Eryk Sun and Oren Milman.

View File

@ -5319,7 +5319,7 @@ cast_check_pointertype(PyObject *arg)
if (PyCFuncPtrTypeObject_Check(arg))
return 1;
dict = PyType_stgdict(arg);
if (dict) {
if (dict != NULL && dict->proto != NULL) {
if (PyUnicode_Check(dict->proto)
&& (strchr("sPzUZXO", PyUnicode_AsUTF8(dict->proto)[0]))) {
/* simple pointer types, c_void_p, c_wchar_p, BSTR, ... */