diff options
author | Martin Panter <vadmium+py@gmail.com> | 2015-11-13 22:12:12 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2015-11-13 22:12:12 (GMT) |
commit | 0da4ac1f21d84bf147353122db2510b90a99d316 (patch) | |
tree | 0cb3032c683323eb21c37ef0dd329972e75bf552 /Modules | |
parent | c73771afd853586bc69863e3bba155349a132f44 (diff) | |
parent | 1bb651540e0743c4e58d875d1de240597862fa34 (diff) | |
download | cpython-0da4ac1f21d84bf147353122db2510b90a99d316.zip cpython-0da4ac1f21d84bf147353122db2510b90a99d316.tar.gz cpython-0da4ac1f21d84bf147353122db2510b90a99d316.tar.bz2 |
Issue #25498: Merge ctypes crash fix from 3.4 into 3.5
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_ctypes/_ctypes.c | 56 |
1 files changed, 38 insertions, 18 deletions
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index b9fd82e..00eb700 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -463,45 +463,65 @@ KeepRef(CDataObject *target, Py_ssize_t index, PyObject *keep); static PyObject * CDataType_from_buffer(PyObject *type, PyObject *args) { - Py_buffer buffer; + PyObject *obj; + PyObject *mv; + PyObject *result; + Py_buffer *buffer; Py_ssize_t offset = 0; - PyObject *result, *mv; + StgDictObject *dict = PyType_stgdict(type); assert (dict); - if (!PyArg_ParseTuple(args, "w*|n:from_buffer", &buffer, &offset)) + if (!PyArg_ParseTuple(args, "O|n:from_buffer", &obj, &offset)) return NULL; + mv = PyMemoryView_FromObject(obj); + if (mv == NULL) + return NULL; + + buffer = PyMemoryView_GET_BUFFER(mv); + + if (buffer->readonly) { + PyErr_SetString(PyExc_TypeError, + "underlying buffer is not writable"); + Py_DECREF(mv); + return NULL; + } + + if (!PyBuffer_IsContiguous(buffer, 'C')) { + PyErr_SetString(PyExc_TypeError, + "underlying buffer is not C contiguous"); + Py_DECREF(mv); + return NULL; + } + if (offset < 0) { PyErr_SetString(PyExc_ValueError, "offset cannot be negative"); - PyBuffer_Release(&buffer); + Py_DECREF(mv); return NULL; } - if (dict->size > buffer.len - offset) { + + if (dict->size > buffer->len - offset) { PyErr_Format(PyExc_ValueError, - "Buffer size too small (%zd instead of at least %zd bytes)", - buffer.len, dict->size + offset); - PyBuffer_Release(&buffer); + "Buffer size too small " + "(%zd instead of at least %zd bytes)", + buffer->len, dict->size + offset); + Py_DECREF(mv); return NULL; } - result = PyCData_AtAddress(type, (char *)buffer.buf + offset); + result = PyCData_AtAddress(type, (char *)buffer->buf + offset); if (result == NULL) { - PyBuffer_Release(&buffer); + Py_DECREF(mv); return NULL; } - mv = PyMemoryView_FromBuffer(&buffer); - if (mv == NULL) { - PyBuffer_Release(&buffer); + if (-1 == KeepRef((CDataObject *)result, -1, mv)) { + Py_DECREF(result); return NULL; } - /* Hack the memoryview so that it will release the buffer. */ - ((PyMemoryViewObject *)mv)->mbuf->master.obj = buffer.obj; - ((PyMemoryViewObject *)mv)->view.obj = buffer.obj; - if (-1 == KeepRef((CDataObject *)result, -1, mv)) - result = NULL; + return result; } |