diff options
author | Sebastian Berg <sebastian@sipsolutions.net> | 2020-05-22 13:32:51 -0500 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2020-05-22 14:17:50 -0500 |
commit | a9652077be95f83f56c9b77e7ad1ed7710516626 (patch) | |
tree | a033f62f7c0fc69bb03d05a4e12148134380d7da /numpy | |
parent | fc2518ba6b11fc52b0ff477b9e83576be90562d8 (diff) | |
download | numpy-a9652077be95f83f56c9b77e7ad1ed7710516626.tar.gz |
BUG: Fix reference count leak in array allocation memory path
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/core/src/multiarray/ctors.c | 10 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 7 |
2 files changed, 12 insertions, 5 deletions
diff --git a/numpy/core/src/multiarray/ctors.c b/numpy/core/src/multiarray/ctors.c index 502ab0ea9..beee985f6 100644 --- a/numpy/core/src/multiarray/ctors.c +++ b/numpy/core/src/multiarray/ctors.c @@ -869,7 +869,7 @@ discover_dimensions(PyObject *obj, int *maxndim, npy_intp *d, int check_it, return 0; } -static PyObject * +static void raise_memory_error(int nd, npy_intp const *dims, PyArray_Descr *descr) { static PyObject *exc_type = NULL; @@ -894,12 +894,12 @@ raise_memory_error(int nd, npy_intp const *dims, PyArray_Descr *descr) } PyErr_SetObject(exc_type, exc_value); Py_DECREF(exc_value); - return NULL; + return; fail: /* we couldn't raise the formatted exception for some reason */ PyErr_WriteUnraisable(NULL); - return PyErr_NoMemory(); + PyErr_NoMemory(); } /* @@ -1079,10 +1079,10 @@ PyArray_NewFromDescr_int( data = npy_alloc_cache(nbytes); } if (data == NULL) { - return raise_memory_error(fa->nd, fa->dimensions, descr); + raise_memory_error(fa->nd, fa->dimensions, descr); + goto fail; } fa->flags |= NPY_ARRAY_OWNDATA; - } else { /* diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 2edcc680b..86a8cff0c 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -845,6 +845,13 @@ class TestCreation: assert_raises(ValueError, np.zeros, shape, dtype=np.int8) assert_raises(ValueError, np.ones, shape, dtype=np.int8) + @pytest.mark.skipif(np.dtype(np.intp).itemsize != 8, + reason="malloc may not fail on 32 bit systems") + def test_malloc_fails(self): + # This test is guaranteed to fail due to a too large allocation + with assert_raises(np.core._exceptions._ArrayMemoryError): + np.empty(np.iinfo(np.intp).max, dtype=np.uint8) + def test_zeros(self): types = np.typecodes['AllInteger'] + np.typecodes['AllFloat'] for dt in types: |