diff options
author | orenmn <orenmn@users.noreply.github.com> | 2017-03-02 17:42:40 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2017-03-02 17:42:40 (GMT) |
commit | 1bea762d9ec823544c530d567330a47f64d93d4f (patch) | |
tree | e147eaf429f75a16d3e952e035fd7432b076ab95 /Modules | |
parent | 8ae264ce6dfcd6923d7bbde0e975389bea7d9881 (diff) | |
download | cpython-1bea762d9ec823544c530d567330a47f64d93d4f.zip cpython-1bea762d9ec823544c530d567330a47f64d93d4f.tar.gz cpython-1bea762d9ec823544c530d567330a47f64d93d4f.tar.bz2 |
bpo-28129: fix ctypes crashes (#386)
* init commit, with initial tests for from_param and fields __set__ and __get__, and some additions to from_buffer and from_buffer_copy
* added the rest of tests and patches. probably only a first draft.
* removed trailing spaces
* replace ctype with ctypes in error messages
* change back from ctypes instance to ctype instance
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_ctypes/_ctypes.c | 31 | ||||
-rw-r--r-- | Modules/_ctypes/cfield.c | 12 |
2 files changed, 34 insertions, 9 deletions
diff --git a/Modules/_ctypes/_ctypes.c b/Modules/_ctypes/_ctypes.c index 833749a..9d42109 100644 --- a/Modules/_ctypes/_ctypes.c +++ b/Modules/_ctypes/_ctypes.c @@ -924,6 +924,7 @@ PyCPointerType_new(PyTypeObject *type, PyObject *args, PyObject *kwds) if (proto) { StgDictObject *itemdict = PyType_stgdict(proto); const char *current_format; + /* PyCPointerType_SetProto has verified proto has a stgdict. */ assert(itemdict); /* If itemdict->format is NULL, then this is a pointer to an incomplete type. We create a generic format string @@ -970,7 +971,11 @@ PyCPointerType_set_type(PyTypeObject *self, PyObject *type) StgDictObject *dict; dict = PyType_stgdict((PyObject *)self); - assert(dict); + if (!dict) { + PyErr_SetString(PyExc_TypeError, + "abstract class"); + return NULL; + } if (-1 == PyCPointerType_SetProto(dict, type)) return NULL; @@ -995,7 +1000,11 @@ PyCPointerType_from_param(PyObject *type, PyObject *value) } typedict = PyType_stgdict(type); - assert(typedict); /* Cannot be NULL for pointer types */ + if (!typedict) { + PyErr_SetString(PyExc_TypeError, + "abstract class"); + return NULL; + } /* If we expect POINTER(<type>), but receive a <type> instance, accept it by calling byref(<type>). @@ -2057,7 +2066,11 @@ PyCSimpleType_from_param(PyObject *type, PyObject *value) } dict = PyType_stgdict(type); - assert(dict); + if (!dict) { + PyErr_SetString(PyExc_TypeError, + "abstract class"); + return NULL; + } /* I think we can rely on this being a one-character string */ fmt = PyUnicode_AsUTF8(dict->proto); @@ -3146,7 +3159,11 @@ _validate_paramflags(PyTypeObject *type, PyObject *paramflags) PyObject *argtypes; dict = PyType_stgdict((PyObject *)type); - assert(dict); /* Cannot be NULL. 'type' is a PyCFuncPtr type. */ + if (!dict) { + PyErr_SetString(PyExc_TypeError, + "abstract class"); + return 0; + } argtypes = dict->argtypes; if (paramflags == NULL || dict->argtypes == NULL) @@ -4779,7 +4796,7 @@ Pointer_ass_item(PyObject *myself, Py_ssize_t index, PyObject *value) } stgdict = PyObject_stgdict((PyObject *)self); - assert(stgdict); /* Cannot be NULL fr pointer instances */ + assert(stgdict); /* Cannot be NULL for pointer instances */ proto = stgdict->proto; assert(proto); @@ -4807,7 +4824,7 @@ Pointer_get_contents(CDataObject *self, void *closure) } stgdict = PyObject_stgdict((PyObject *)self); - assert(stgdict); /* Cannot be NULL fr pointer instances */ + assert(stgdict); /* Cannot be NULL for pointer instances */ return PyCData_FromBaseObj(stgdict->proto, (PyObject *)self, 0, *(void **)self->b_ptr); @@ -4826,7 +4843,7 @@ Pointer_set_contents(CDataObject *self, PyObject *value, void *closure) return -1; } stgdict = PyObject_stgdict((PyObject *)self); - assert(stgdict); /* Cannot be NULL fr pointer instances */ + assert(stgdict); /* Cannot be NULL for pointer instances */ assert(stgdict->proto); if (!CDataObject_Check(value)) { int res = PyObject_IsInstance(value, stgdict->proto); diff --git a/Modules/_ctypes/cfield.c b/Modules/_ctypes/cfield.c index bb9b115..e2b9aa8 100644 --- a/Modules/_ctypes/cfield.c +++ b/Modules/_ctypes/cfield.c @@ -205,7 +205,11 @@ PyCField_set(CFieldObject *self, PyObject *inst, PyObject *value) { CDataObject *dst; char *ptr; - assert(CDataObject_Check(inst)); + if (!CDataObject_Check(inst)) { + PyErr_SetString(PyExc_TypeError, + "not a ctype instance"); + return -1; + } dst = (CDataObject *)inst; ptr = dst->b_ptr + self->offset; if (value == NULL) { @@ -225,7 +229,11 @@ PyCField_get(CFieldObject *self, PyObject *inst, PyTypeObject *type) Py_INCREF(self); return (PyObject *)self; } - assert(CDataObject_Check(inst)); + if (!CDataObject_Check(inst)) { + PyErr_SetString(PyExc_TypeError, + "not a ctype instance"); + return NULL; + } src = (CDataObject *)inst; return PyCData_get(self->proto, self->getfunc, inst, self->index, self->size, src->b_ptr + self->offset); |