]> granicus.if.org Git - python/commitdiff
Issue #12764: Fix a crash in ctypes when the name of a Structure field is not
authorAmaury Forgeot d'Arc <amauryfa@gmail.com>
Fri, 2 Sep 2011 18:39:40 +0000 (20:39 +0200)
committerAmaury Forgeot d'Arc <amauryfa@gmail.com>
Fri, 2 Sep 2011 18:39:40 +0000 (20:39 +0200)
a string.

Lib/ctypes/test/test_structures.py
Misc/NEWS
Modules/_ctypes/stgdict.c

index 536ea50cd8f84a49e09a8beeca5b1c0413e5486e..e4530d5a759bcf9c17b611fcc052400ec5d2e666 100644 (file)
@@ -239,6 +239,14 @@ class StructureTestCase(unittest.TestCase):
             pass
         self.assertRaises(TypeError, setattr, POINT, "_fields_", [("x", 1), ("y", 2)])
 
+    def test_invalid_name(self):
+        # field name must be string
+        def declare_with_name(name):
+            class S(Structure):
+                _fields_ = [(name, c_int)]
+
+        self.assertRaises(TypeError, declare_with_name, b"x")
+
     def test_intarray_fields(self):
         class SomeInts(Structure):
             _fields_ = [("a", c_int * 4)]
index c74cb5596d71f7f406ea39ae081fe1a69f5e177a..f489ab257eae93e99d70f00619466a3c19a7fa06 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -193,6 +193,9 @@ Library
 Extension Modules
 -----------------
 
+- Issue #12764: Fix a crash in ctypes when the name of a Structure field is not
+  a string.
+
 - Issue #11241: subclasses of ctypes.Array can now be subclassed.
 
 - Issue #9651: Fix a crash when ctypes.create_string_buffer(0) was passed to
index 105e0df3ec89e91c754a28d06ecfa2483a0cd4fe..14dc16fc0297833ae03e5097578db4a70cf4aaf2 100644 (file)
@@ -482,8 +482,21 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct
             char *fieldfmt = dict->format ? dict->format : "B";
             char *fieldname = _PyUnicode_AsString(name);
             char *ptr;
-            Py_ssize_t len = strlen(fieldname) + strlen(fieldfmt);
-            char *buf = alloca(len + 2 + 1);
+            Py_ssize_t len; 
+            char *buf;
+
+            if (fieldname == NULL)
+            {
+                PyErr_Format(PyExc_TypeError,
+                             "structure field name must be string not %s",
+                             name->ob_type->tp_name);
+                                
+                Py_DECREF(pair);
+                return -1;
+            }
+
+            len = strlen(fieldname) + strlen(fieldfmt);
+            buf = alloca(len + 2 + 1);
 
             sprintf(buf, "%s:%s:", fieldfmt, fieldname);