Skip to content

Commit 04b3575

Browse files
committed
Issue #25659: Merge ctypes fix from 3.5
2 parents e45df0a + 6e723d2 commit 04b3575

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

Lib/ctypes/test/test_frombuffer.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,13 @@ def test_from_buffer_copy_with_offset(self):
120120
with self.assertRaises(ValueError):
121121
(c_int * 1).from_buffer_copy(a, 16 * sizeof(c_int))
122122

123+
def test_abstract(self):
124+
self.assertRaises(TypeError, Array.from_buffer, bytearray(10))
125+
self.assertRaises(TypeError, Structure.from_buffer, bytearray(10))
126+
self.assertRaises(TypeError, Union.from_buffer, bytearray(10))
127+
self.assertRaises(TypeError, Array.from_buffer_copy, b"123")
128+
self.assertRaises(TypeError, Structure.from_buffer_copy, b"123")
129+
self.assertRaises(TypeError, Union.from_buffer_copy, b"123")
130+
123131
if __name__ == '__main__':
124132
unittest.main()

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ Core and Builtins
4040
Library
4141
-------
4242

43+
- Issue #25659: In ctypes, prevent a crash calling the from_buffer() and
44+
from_buffer_copy() methods on abstract classes like Array.
45+
4346
- Issue #19717: Makes Path.resolve() succeed on paths that do not exist.
4447
Patch by Vajrasky Kok
4548

Modules/_ctypes/_ctypes.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,10 @@ CDataType_from_buffer(PyObject *type, PyObject *args)
463463
Py_ssize_t offset = 0;
464464

465465
StgDictObject *dict = PyType_stgdict(type);
466-
assert (dict);
466+
if (!dict) {
467+
PyErr_SetString(PyExc_TypeError, "abstract class");
468+
return NULL;
469+
}
467470

468471
if (!PyArg_ParseTuple(args, "O|n:from_buffer", &obj, &offset))
469472
return NULL;
@@ -531,9 +534,12 @@ CDataType_from_buffer_copy(PyObject *type, PyObject *args)
531534
Py_ssize_t offset = 0;
532535
PyObject *result;
533536
StgDictObject *dict = PyType_stgdict(type);
534-
assert (dict);
537+
if (!dict) {
538+
PyErr_SetString(PyExc_TypeError, "abstract class");
539+
return NULL;
540+
}
535541

536-
if (!PyArg_ParseTuple(args, "y*|n:from_buffer", &buffer, &offset))
542+
if (!PyArg_ParseTuple(args, "y*|n:from_buffer_copy", &buffer, &offset))
537543
return NULL;
538544

539545
if (offset < 0) {

0 commit comments

Comments
 (0)