diff options
author | Alexandre de Siqueira <alex.desiqueira@igdore.org> | 2022-02-16 08:46:02 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-16 10:46:02 -0600 |
commit | 935fe83ddaa3250d176bc848579ffdc4e1017090 (patch) | |
tree | 75a5b0ec027cfcd6633e4cf6d9de801357a8d96a | |
parent | f80aba83de3027a08597b13107e549f401a2ec40 (diff) | |
download | numpy-935fe83ddaa3250d176bc848579ffdc4e1017090.tar.gz |
BUG: Fix unpickling an empty ndarray with a none-zero dimension (#21067)
Changing num to the number of bytes in the input array, PyArray_NBYTES(self). Solves #21009.
* Fixing nbyte size in methods.c:memcpy
* Adding a test
* Re-adding removed newline
* Shrinking the test array to save memory
-rw-r--r-- | numpy/core/src/multiarray/methods.c | 2 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 9 |
2 files changed, 10 insertions, 1 deletions
diff --git a/numpy/core/src/multiarray/methods.c b/numpy/core/src/multiarray/methods.c index 792686cf0..bd6318206 100644 --- a/numpy/core/src/multiarray/methods.c +++ b/numpy/core/src/multiarray/methods.c @@ -2205,7 +2205,7 @@ array_setstate(PyArrayObject *self, PyObject *args) Py_DECREF(typecode); } else { - memcpy(PyArray_DATA(self), datastr, num); + memcpy(PyArray_DATA(self), datastr, PyArray_NBYTES(self)); } PyArray_ENABLEFLAGS(self, NPY_ARRAY_OWNDATA); fa->base = NULL; diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 137fb02ec..6ba90a97f 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -1635,6 +1635,15 @@ class TestZeroSizeFlexible: assert_equal(zs.dtype, zs2.dtype) + def test_pickle_empty(self): + """Checking if an empty array pickled and un-pickled will not cause a + segmentation fault""" + arr = np.array([]).reshape(999999, 0) + pk_dmp = pickle.dumps(arr) + pk_load = pickle.loads(pk_dmp) + + assert pk_load.size == 0 + @pytest.mark.skipif(pickle.HIGHEST_PROTOCOL < 5, reason="requires pickle protocol 5") def test_pickle_with_buffercallback(self): |