Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 817905b

Browse files
committed
#13096: Fix segfault in CTypes POINTER handling of large values.
Patch by Meador Inge.
1 parent 4cfb5be commit 817905b

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

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, int,
99
int, int, int, int, 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
@@ -27,6 +27,9 @@ Core and Builtins
2727
Library
2828
-------
2929

30+
- Issue #13096: Fixed segfault in CTypes POINTER handling of large
31+
values.
32+
3033
- Issue #11694: Raise ConversionError in xdrlib as documented. Patch
3134
by Filip Gruszczyński and Claudiu Popa.
3235

Modules/_ctypes/callproc.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,24 +1672,30 @@ POINTER(PyObject *self, PyObject *cls)
16721672
}
16731673
if (PyUnicode_CheckExact(cls)) {
16741674
char *name = _PyUnicode_AsString(cls);
1675-
buf = alloca(strlen(name) + 3 + 1);
1675+
buf = PyMem_Malloc(strlen(name) + 3 + 1);
1676+
if (buf == NULL)
1677+
return PyErr_NoMemory();
16761678
sprintf(buf, "LP_%s", name);
16771679
result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type),
16781680
"s(O){}",
16791681
buf,
16801682
&PyCPointer_Type);
1683+
PyMem_Free(buf);
16811684
if (result == NULL)
16821685
return result;
16831686
key = PyLong_FromVoidPtr(result);
16841687
} else if (PyType_Check(cls)) {
16851688
typ = (PyTypeObject *)cls;
1686-
buf = alloca(strlen(typ->tp_name) + 3 + 1);
1689+
buf = PyMem_Malloc(strlen(typ->tp_name) + 3 + 1);
1690+
if (buf == NULL)
1691+
return PyErr_NoMemory();
16871692
sprintf(buf, "LP_%s", typ->tp_name);
16881693
result = PyObject_CallFunction((PyObject *)Py_TYPE(&PyCPointer_Type),
16891694
"s(O){sO}",
16901695
buf,
16911696
&PyCPointer_Type,
16921697
"_type_", cls);
1698+
PyMem_Free(buf);
16931699
if (result == NULL)
16941700
return result;
16951701
Py_INCREF(cls);

0 commit comments

Comments
 (0)