diff options
author | Charles Harris <charlesr.harris@gmail.com> | 2012-02-12 19:22:12 -0700 |
---|---|---|
committer | Travis E. Oliphant <teoliphant@gmail.com> | 2012-02-12 23:58:45 -0600 |
commit | 7e202a2dd96a43579dfe82defe7ec7b63ffdd3ab (patch) | |
tree | b3e658f914015d83ecfeb1b6dd68c33e9738cfca | |
parent | 015cada57ddd00d866e2a8ef396f83582c192884 (diff) | |
download | numpy-7e202a2dd96a43579dfe82defe7ec7b63ffdd3ab.tar.gz |
BUG: ticket #2046, deleting array attributes causes segfault.
Add check for NULL in setter functions and return Attribute error
if found.
-rw-r--r-- | numpy/core/src/multiarray/getset.c | 40 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 11 |
2 files changed, 49 insertions, 2 deletions
diff --git a/numpy/core/src/multiarray/getset.c b/numpy/core/src/multiarray/getset.c index bcd6f207e..14042c9af 100644 --- a/numpy/core/src/multiarray/getset.c +++ b/numpy/core/src/multiarray/getset.c @@ -44,6 +44,11 @@ array_shape_set(PyArrayObject *self, PyObject *val) int nd; PyArrayObject *ret; + if (val == NULL) { + PyErr_SetString(PyExc_AttributeError, + "Cannot delete array shape"); + return -1; + } /* Assumes C-order */ ret = (PyArrayObject *)PyArray_Reshape(self, val); if (ret == NULL) { @@ -101,6 +106,11 @@ array_strides_set(PyArrayObject *self, PyObject *obj) Py_ssize_t buf_len; char *buf; + if (obj == NULL) { + PyErr_SetString(PyExc_AttributeError, + "Cannot delete array strides"); + return -1; + } if (!PyArray_IntpConverter(obj, &newstrides) || newstrides.ptr == NULL) { PyErr_SetString(PyExc_TypeError, "invalid strides"); @@ -311,6 +321,11 @@ array_data_set(PyArrayObject *self, PyObject *op) Py_ssize_t buf_len; int writeable=1; + if (op == NULL) { + PyErr_SetString(PyExc_AttributeError, + "Cannot delete array data"); + return -1; + } if (PyObject_AsWriteBuffer(op, &buf, &buf_len) < 0) { writeable = 0; if (PyObject_AsReadBuffer(op, (const void **)&buf, &buf_len) < 0) { @@ -408,6 +423,12 @@ array_descr_set(PyArrayObject *self, PyObject *arg) int i; char *msg = "new type not compatible with array."; + if (arg == NULL) { + PyErr_SetString(PyExc_AttributeError, + "Cannot delete array dtype"); + return -1; + } + if (!(PyArray_DescrConverter(arg, &newtype)) || newtype == NULL) { PyErr_SetString(PyExc_TypeError, "invalid data-type for array"); @@ -674,6 +695,11 @@ array_real_set(PyArrayObject *self, PyObject *val) PyArrayObject *new; int retcode; + if (val == NULL) { + PyErr_SetString(PyExc_AttributeError, + "Cannot delete array real part"); + return -1; + } if (PyArray_ISCOMPLEX(self)) { ret = _get_part(self, 0); if (ret == NULL) { @@ -731,6 +757,11 @@ array_imag_get(PyArrayObject *self) static int array_imag_set(PyArrayObject *self, PyObject *val) { + if (val == NULL) { + PyErr_SetString(PyExc_AttributeError, + "Cannot delete array imaginary part"); + return -1; + } if (PyArray_ISCOMPLEX(self)) { PyArrayObject *ret; PyArrayObject *new; @@ -751,8 +782,8 @@ array_imag_set(PyArrayObject *self, PyObject *val) return retcode; } else { - PyErr_SetString(PyExc_TypeError, "array does not have "\ - "imaginary part to set"); + PyErr_SetString(PyExc_TypeError, + "array does not have imaginary part to set"); return -1; } } @@ -773,6 +804,11 @@ array_flat_set(PyArrayObject *self, PyObject *val) int swap; PyArray_CopySwapFunc *copyswap; + if (val == NULL) { + PyErr_SetString(PyExc_AttributeError, + "Cannot delete array flat iterator"); + return -1; + } typecode = PyArray_DESCR(self); Py_INCREF(typecode); arr = (PyArrayObject *)PyArray_FromAny(val, typecode, diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 6641a99f0..6d402c3ec 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -2307,5 +2307,16 @@ if sys.version_info >= (2, 6): x3 = np.arange(dt3.itemsize, dtype=np.int8).view(dt3) self._check_roundtrip(x3) + + class TestArrayAttributeDeletion(object): + """ticket #2046, should not seqfault, raise AttributeError""" + + def test_attribute_deletion(self): + a = np.ones(2) + attr = ['shape', 'strides', 'data', 'dtype', 'real', 'imag', 'flat'] + for s in attr: + assert_raises(AttributeError, delattr, a, s) + + if __name__ == "__main__": run_module_suite() |