Skip to content

Commit 9a34be0

Browse files
committed
BUG: ticket numpy#2046, deleting array attributes causes segfault.
Add check for NULL in setter functions and return Attribute error if found.
1 parent 666c02d commit 9a34be0

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎numpy/core/src/multiarray/getset.c‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ array_shape_set(PyArrayObject *self, PyObject *val)
4444
int nd;
4545
PyObject *ret;
4646

47+
if (val == NULL) {
48+
PyErr_SetString(PyExc_AttributeError,
49+
"Cannot delete array shape");
50+
return -1;
51+
}
4752
/* Assumes C-order */
4853
ret = PyArray_Reshape(self, val);
4954
if (ret == NULL) {
@@ -99,6 +104,11 @@ array_strides_set(PyArrayObject *self, PyObject *obj)
99104
Py_ssize_t buf_len;
100105
char *buf;
101106

107+
if (obj == NULL) {
108+
PyErr_SetString(PyExc_AttributeError,
109+
"Cannot delete array strides");
110+
return -1;
111+
}
102112
if (!PyArray_IntpConverter(obj, &newstrides) ||
103113
newstrides.ptr == NULL) {
104114
PyErr_SetString(PyExc_TypeError, "invalid strides");
@@ -305,6 +315,11 @@ array_data_set(PyArrayObject *self, PyObject *op)
305315
Py_ssize_t buf_len;
306316
int writeable=1;
307317

318+
if (op == NULL) {
319+
PyErr_SetString(PyExc_AttributeError,
320+
"Cannot delete array data");
321+
return -1;
322+
}
308323
if (PyObject_AsWriteBuffer(op, &buf, &buf_len) < 0) {
309324
writeable = 0;
310325
if (PyObject_AsReadBuffer(op, (const void **)&buf, &buf_len) < 0) {
@@ -401,6 +416,12 @@ array_descr_set(PyArrayObject *self, PyObject *arg)
401416
int index;
402417
char *msg = "new type not compatible with array.";
403418

419+
if (arg == NULL) {
420+
PyErr_SetString(PyExc_AttributeError,
421+
"Cannot delete array dtype");
422+
return -1;
423+
}
424+
404425
if (!(PyArray_DescrConverter(arg, &newtype)) ||
405426
newtype == NULL) {
406427
PyErr_SetString(PyExc_TypeError, "invalid data-type for array");
@@ -665,6 +686,11 @@ array_real_set(PyArrayObject *self, PyObject *val)
665686
PyArrayObject *new;
666687
int rint;
667688

689+
if (val == NULL) {
690+
PyErr_SetString(PyExc_AttributeError,
691+
"Cannot delete array real part");
692+
return -1;
693+
}
668694
if (PyArray_ISCOMPLEX(self)) {
669695
ret = _get_part(self, 0);
670696
if (ret == NULL) {
@@ -722,6 +748,11 @@ array_imag_get(PyArrayObject *self)
722748
static int
723749
array_imag_set(PyArrayObject *self, PyObject *val)
724750
{
751+
if (val == NULL) {
752+
PyErr_SetString(PyExc_AttributeError,
753+
"Cannot delete array imaginary part");
754+
return -1;
755+
}
725756
if (PyArray_ISCOMPLEX(self)) {
726757
PyArrayObject *ret;
727758
PyArrayObject *new;
@@ -742,8 +773,8 @@ array_imag_set(PyArrayObject *self, PyObject *val)
742773
return rint;
743774
}
744775
else {
745-
PyErr_SetString(PyExc_TypeError, "array does not have "\
746-
"imaginary part to set");
776+
PyErr_SetString(PyExc_TypeError,
777+
"array does not have imaginary part to set");
747778
return -1;
748779
}
749780
}
@@ -764,7 +795,12 @@ array_flat_set(PyArrayObject *self, PyObject *val)
764795
int swap;
765796
PyArray_CopySwapFunc *copyswap;
766797

767-
typecode = self->descr;
798+
if (val == NULL) {
799+
PyErr_SetString(PyExc_AttributeError,
800+
"Cannot delete array flat iterator");
801+
return -1;
802+
}
803+
typecode = PyArray_DESCR(self);
768804
Py_INCREF(typecode);
769805
arr = PyArray_FromAny(val, typecode,
770806
0, 0, FORCECAST | FORTRAN_IF(self), NULL);

‎numpy/core/tests/test_multiarray.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,5 +2250,16 @@ def test_padded_struct_array(self):
22502250
x3 = np.arange(dt3.itemsize, dtype=np.int8).view(dt3)
22512251
self._check_roundtrip(x3)
22522252

2253+
2254+
class TestArrayAttributeDeletion(object):
2255+
"""ticket #2046, should not seqfault, raise AttributeError"""
2256+
2257+
def test_attribute_deletion(self):
2258+
a = np.ones(2)
2259+
attr = ['shape', 'strides', 'data', 'dtype', 'real', 'imag', 'flat']
2260+
for s in attr:
2261+
assert_raises(AttributeError, delattr, a, s)
2262+
2263+
22532264
if __name__ == "__main__":
22542265
run_module_suite()

0 commit comments

Comments
 (0)