8000 BUG: raise ValueError for empty arrays passed to _pyarray_correlate (… · numpy/numpy@480dc6b · GitHub
[go: up one dir, main page]

Skip to content

Commit 480dc6b

Browse files
douglasdavismattip
authored andcommitted
BUG: raise ValueError for empty arrays passed to _pyarray_correlate (#14829)
* BUG: raise ValueError for empty arrays passed to _pyarray_correlate This is related to GitHub issue #14366 where empty arrays were causing crashes.
1 parent 8e603c7 commit 480dc6b

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

numpy/core/src/multiarray/multiarraymodule.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,14 @@ _pyarray_correlate(PyArrayObject *ap1, PyArrayObject *ap2, int typenum,
11151115

11161116
n1 = PyArray_DIMS(ap1)[0];
11171117
n2 = PyArray_DIMS(ap2)[0];
1118+
if (n1 == 0) {
1119+
PyErr_SetString(PyExc_ValueError, "first array argument cannot be empty");
1120+
return NULL;
1121+
}
1122+
if (n2 == 0) {
1123+
PyErr_SetString(PyExc_ValueError, "second array argument cannot be empty");
1124+
return NULL;
1125+
}
11181126
if (n1 < n2) {
11191127
ret = ap1;
11201128
ap1 = ap2;

numpy/core/tests/test_numeric.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,6 +2567,11 @@ def test_complex(self):
25672567
z = np.correlate(y, x, mode='full')
25682568
assert_array_almost_equal(z, r_z)
25692569

2570+
def test_zero_size(self):
2571+
with pytest.raises(ValueError):
2572+
np.correlate(np.array([]), np.ones(1000), mode='full')
2573+
with pytest.raises(ValueError):
2574+
np.correlate(np.ones(1000), np.array([]), mode='full')
25702575

25712576
class TestConvolve(object):
25722577
def test_object(self):

0 commit comments

Comments
 (0)
0