[Python-checkins] cpython (2.7): Issue #12764: Fix a crash in ctypes when the name of a Structure field is not
amaury.forgeotdarc
python-checkins at python.org
Fri Sep 2 20:44:15 CEST 2011
http://hg.python.org/cpython/rev/73827c23cdde
changeset: 72208:73827c23cdde
branch: 2.7
parent: 72203:fe60689d6a2e
user: Amaury Forgeot d'Arc <amauryfa at gmail.com>
date: Fri Sep 02 20:32:23 2011 +0200
summary:
Issue #12764: Fix a crash in ctypes when the name of a Structure field is not
a string.
files:
Lib/ctypes/test/test_structures.py | 8 ++++++++
Misc/NEWS | 3 +++
Modules/_ctypes/stgdict.c | 17 +++++++++++++++--
3 files changed, 26 insertions(+), 2 deletions(-)
diff --git a/Lib/ctypes/test/test_structures.py b/Lib/ctypes/test/test_structures.py
--- a/Lib/ctypes/test/test_structures.py
+++ b/Lib/ctypes/test/test_structures.py
@@ -239,6 +239,14 @@
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
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -180,6 +180,9 @@
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
--- a/Modules/_ctypes/stgdict.c
+++ b/Modules/_ctypes/stgdict.c
@@ -494,8 +494,21 @@
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);
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list