Skip to content

Commit

Permalink
BUG: Fix unpickling an empty ndarray with a none-zero dimension (#21067)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Alexandre de Siqueira authored and charris committed Mar 2, 2022
1 parent 593cc07 commit 03d842e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion numpy/core/src/multiarray/methods.c
Original file line number Diff line number Diff line change
Expand Up @@ -2175,7 +2175,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;
Expand Down
9 changes: 9 additions & 0 deletions numpy/core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,15 @@ def test_pickle(self):

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):
Expand Down

0 comments on commit 03d842e

Please sign in to comment.