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

Skip to content

Commit 7d6e923

Browse files
committed
Issue #25659: Merge ctypes fix from 3.6
2 parents a57890e + 04b3575 commit 7d6e923

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
@@ -131,6 +131,9 @@ Core and Builtins
131131
Library
132132
-------
133133

134+
- Issue #25659: In ctypes, prevent a crash calling the from_buffer() and
135+
from_buffer_copy() methods on abstract classes like Array.
136+
134137
- Issue #28548: In the "http.server" module, parse the protocol version if
135138
possible, to avoid using HTTP 0.9 in some error responses.
136139

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)