changeset: 93006:ff59b0f9e142 branch: 2.7 parent: 93001:c4a71e5f2049 user: R David Murray date: Sun Oct 12 14:26:30 2014 -0400 files: Lib/ctypes/test/test_pointers.py Misc/NEWS Modules/_ctypes/callproc.c description: #13096: Fix segfault in CTypes POINTER handling of large values. Patch by Meador Inge. diff -r c4a71e5f2049 -r ff59b0f9e142 Lib/ctypes/test/test_pointers.py --- a/Lib/ctypes/test/test_pointers.py Sun Oct 12 20:32:05 2014 +0300 +++ b/Lib/ctypes/test/test_pointers.py Sun Oct 12 14:26:30 2014 -0400 @@ -7,6 +7,8 @@ c_long, c_ulong, c_longlong, c_ulonglong, c_double, c_float] python_types = [int, int, int, int, int, long, int, long, long, long, float, float] +LargeNamedType = type('T' * 2 ** 25, (Structure,), {}) +large_string = 'T' * 2 ** 25 class PointersTestCase(unittest.TestCase): @@ -188,5 +190,11 @@ mth = WINFUNCTYPE(None)(42, "name", (), None) self.assertEqual(bool(mth), True) + def test_pointer_type_name(self): + self.assertTrue(POINTER(LargeNamedType)) + + def test_pointer_type_str_name(self): + self.assertTrue(POINTER(large_string)) + if __name__ == '__main__': unittest.main() diff -r c4a71e5f2049 -r ff59b0f9e142 Misc/NEWS --- a/Misc/NEWS Sun Oct 12 20:32:05 2014 +0300 +++ b/Misc/NEWS Sun Oct 12 14:26:30 2014 -0400 @@ -37,6 +37,9 @@ Library ------- +- Issue #13096: Fixed segfault in CTypes POINTER handling of large + values. + - Issue #11694: Raise ConversionError in xdrlib as documented. Patch by Filip GruszczyƄski and Claudiu Popa. diff -r c4a71e5f2049 -r ff59b0f9e142 Modules/_ctypes/callproc.c --- a/Modules/_ctypes/callproc.c Sun Oct 12 20:32:05 2014 +0300 +++ b/Modules/_ctypes/callproc.c Sun Oct 12 14:26:30 2014 -0400 @@ -1807,7 +1807,9 @@ return result; } if (PyString_CheckExact(cls)) { - buf = alloca(strlen(PyString_AS_STRING(cls)) + 3 + 1); + buf = PyMem_Malloc(strlen(PyString_AS_STRING(cls)) + 3 + 1); + if (buf == NULL) + return PyErr_NoMemory(); sprintf(buf, "LP_%s", PyString_AS_STRING(cls)); result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type), "s(O){}", @@ -1818,13 +1820,16 @@ key = PyLong_FromVoidPtr(result); } else if (PyType_Check(cls)) { typ = (PyTypeObject *)cls; - buf = alloca(strlen(typ->tp_name) + 3 + 1); + buf = PyMem_Malloc(strlen(typ->tp_name) + 3 + 1); + if (buf == NULL) + return PyErr_NoMemory(); sprintf(buf, "LP_%s", typ->tp_name); result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type), "s(O){sO}", buf, &PyCPointer_Type, "_type_", cls); + PyMem_Free(buf); if (result == NULL) return result; Py_INCREF(cls);