]> granicus.if.org Git - python/commitdiff
bpo-21983: Fix a crash in ctypes.cast() when passed a ctypes structured data type...
authorOren Milman <orenmn@gmail.com>
Wed, 9 May 2018 21:38:56 +0000 (00:38 +0300)
committerSteve Dower <steve.dower@microsoft.com>
Wed, 9 May 2018 21:38:56 +0000 (14:38 -0700)
Lib/ctypes/test/test_cast.py
Misc/NEWS.d/next/Core and Builtins/2017-10-02-21-02-14.bpo-21983.UoC319.rst [new file with mode: 0644]
Modules/_ctypes/_ctypes.c

index 187d2bde143d821a7c91feb3f1fa6d0cd4eb4c2a..6878f9732826f116f8d6048c78222f54e6749f88 100644 (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()
diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-10-02-21-02-14.bpo-21983.UoC319.rst b/Misc/NEWS.d/next/Core and Builtins/2017-10-02-21-02-14.bpo-21983.UoC319.rst
new file mode 100644 (file)
index 0000000..88a0368
--- /dev/null
@@ -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.
index 93e8d8d4233075bf4cc46bb320c27fbfd76e53c7..5bf49acb6c2d17072f73f66d3e51a34bd931622c 100644 (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, ... */