Skip to content

Commit

Permalink
Issue #25659: Merge ctypes fix from 3.6
Browse files Browse the repository at this point in the history
  • Loading branch information
vadmium committed Nov 20, 2016
2 parents 3471793 + 18e9af9 commit 7455ba8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
8 changes: 8 additions & 0 deletions Lib/ctypes/test/test_frombuffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,13 @@ def test_from_buffer_copy_with_offset(self):
with self.assertRaises(ValueError):
(c_int * 1).from_buffer_copy(a, 16 * sizeof(c_int))

def test_abstract(self):
self.assertRaises(TypeError, Array.from_buffer, bytearray(10))
self.assertRaises(TypeError, Structure.from_buffer, bytearray(10))
self.assertRaises(TypeError, Union.from_buffer, bytearray(10))
self.assertRaises(TypeError, Array.from_buffer_copy, b"123")
self.assertRaises(TypeError, Structure.from_buffer_copy, b"123")
self.assertRaises(TypeError, Union.from_buffer_copy, b"123")

if __name__ == '__main__':
unittest.main()
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ Core and Builtins
Library
-------

- Issue #25659: In ctypes, prevent a crash calling the from_buffer() and
from_buffer_copy() methods on abstract classes like Array.

- Issue #28548: In the "http.server" module, parse the protocol version if
possible, to avoid using HTTP 0.9 in some error responses.

Expand Down
12 changes: 9 additions & 3 deletions Modules/_ctypes/_ctypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,10 @@ CDataType_from_buffer(PyObject *type, PyObject *args)
Py_ssize_t offset = 0;

StgDictObject *dict = PyType_stgdict(type);
assert (dict);
if (!dict) {
PyErr_SetString(PyExc_TypeError, "abstract class");
return NULL;
}

if (!PyArg_ParseTuple(args, "O|n:from_buffer", &obj, &offset))
return NULL;
Expand Down Expand Up @@ -531,9 +534,12 @@ CDataType_from_buffer_copy(PyObject *type, PyObject *args)
Py_ssize_t offset = 0;
PyObject *result;
StgDictObject *dict = PyType_stgdict(type);
assert (dict);
if (!dict) {
PyErr_SetString(PyExc_TypeError, "abstract class");
return NULL;
}

if (!PyArg_ParseTuple(args, "y*|n:from_buffer", &buffer, &offset))
if (!PyArg_ParseTuple(args, "y*|n:from_buffer_copy", &buffer, &offset))
return NULL;

if (offset < 0) {
Expand Down

0 comments on commit 7455ba8

Please sign in to comment.