Skip to content

Commit 05cfcc6

Browse files
committed
#13096: Fix segfault in CTypes POINTER handling of large values.
Patch by Meador Inge.
1 parent 931af45 commit 05cfcc6

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

Lib/ctypes/test/test_pointers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
c_long, c_ulong, c_longlong, c_ulonglong, c_double, c_float]
88
python_types = [int, int, int, int, int, long,
99
int, long, long, long, float, float]
10+
LargeNamedType = type('T' * 2 ** 25, (Structure,), {})
11+
large_string = 'T' * 2 ** 25
1012

1113
class PointersTestCase(unittest.TestCase):
1214

@@ -188,5 +190,11 @@ def test_pointers_bool(self):
188190
mth = WINFUNCTYPE(None)(42, "name", (), None)
189191
self.assertEqual(bool(mth), True)
190192

193+
def test_pointer_type_name(self):
194+
self.assertTrue(POINTER(LargeNamedType))
195+
196+
def test_pointer_type_str_name(self):
197+
self.assertTrue(POINTER(large_string))
198+
191199
if __name__ == '__main__':
192200
unittest.main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ Core and Builtins
3737
Library
3838
-------
3939

40+
- Issue #13096: Fixed segfault in CTypes POINTER handling of large
41+
values.
42+
4043
- Issue #11694: Raise ConversionError in xdrlib as documented. Patch
4144
by Filip Gruszczyński and Claudiu Popa.
4245

Modules/_ctypes/callproc.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,9 @@ POINTER(PyObject *self, PyObject *cls)
18071807
return result;
18081808
}
18091809
if (PyString_CheckExact(cls)) {
1810-
buf = alloca(strlen(PyString_AS_STRING(cls)) + 3 + 1);
1810+
buf = PyMem_Malloc(strlen(PyString_AS_STRING(cls)) + 3 + 1);
1811+
if (buf == NULL)
1812+
return PyErr_NoMemory();
18111813
sprintf(buf, "LP_%s", PyString_AS_STRING(cls));
18121814
result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type),
18131815
"s(O){}",
@@ -1818,13 +1820,16 @@ POINTER(PyObject *self, PyObject *cls)
18181820
key = PyLong_FromVoidPtr(result);
18191821
} else if (PyType_Check(cls)) {
18201822
typ = (PyTypeObject *)cls;
1821-
buf = alloca(strlen(typ->tp_name) + 3 + 1);
1823+
buf = PyMem_Malloc(strlen(typ->tp_name) + 3 + 1);
1824+
if (buf == NULL)
1825+
return PyErr_NoMemory();
18221826
sprintf(buf, "LP_%s", typ->tp_name);
18231827
result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type),
18241828
"s(O){sO}",
18251829
buf,
18261830
&PyCPointer_Type,
18271831
"_type_", cls);
1832+
PyMem_Free(buf);
18281833
if (result == NULL)
18291834
return result;
18301835
Py_INCREF(cls);

0 commit comments

Comments
 (0)