From: Amaury Forgeot d'Arc Date: Fri, 2 Sep 2011 18:32:23 +0000 (+0200) Subject: Issue #12764: Fix a crash in ctypes when the name of a Structure field is not X-Git-Tag: v2.7.3rc1~471 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=cf316a171e562a7bb7490749818ddb3174a295b1;p=thirdparty%2FPython%2Fcpython.git Issue #12764: Fix a crash in ctypes when the name of a Structure field is not a string. --- diff --git a/Lib/ctypes/test/test_structures.py b/Lib/ctypes/test/test_structures.py index 77cfb2671191..a84bae0640b0 100644 --- a/Lib/ctypes/test/test_structures.py +++ b/Lib/ctypes/test/test_structures.py @@ -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, u"x\xe9") + def test_intarray_fields(self): class SomeInts(Structure): _fields_ = [("a", c_int * 4)] diff --git a/Misc/NEWS b/Misc/NEWS index 8c77c9f91fb8..6d57532578ac 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -180,6 +180,9 @@ Library Extension Modules ----------------- +- Issue #12764: Fix a crash in ctypes when the name of a Structure field is not + a string. + - Issue #9651: Fix a crash when ctypes.create_string_buffer(0) was passed to some functions like file.write(). diff --git a/Modules/_ctypes/stgdict.c b/Modules/_ctypes/stgdict.c index 4e7ea12fb34d..4d4ecc471746 100644 --- a/Modules/_ctypes/stgdict.c +++ b/Modules/_ctypes/stgdict.c @@ -494,8 +494,21 @@ PyCStructUnionType_update_stgdict(PyObject *type, PyObject *fields, int isStruct char *fieldfmt = dict->format ? dict->format : "B"; char *fieldname = PyString_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);