Skip to content

Commit 18d157a

Browse files
authored
Merge pull request #29237 from charris/backport-29231
BUG: Enforce integer limitation in concatenate (#29231)
2 parents 70b6cda + 6fcbaca commit 18d157a

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

numpy/_core/src/multiarray/multiarraymodule.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,10 +671,17 @@ PyArray_ConcatenateInto(PyObject *op,
671671
}
672672

673673
/* Convert the input list into arrays */
674-
narrays = PySequence_Size(op);
675-
if (narrays < 0) {
674+
Py_ssize_t narrays_true = PySequence_Size(op);
675+
if (narrays_true < 0) {
676676
return NULL;
677677
}
678+
else if (narrays_true > NPY_MAX_INT) {
679+
PyErr_Format(PyExc_ValueError,
680+
"concatenate() only supports up to %d arrays but got %zd.",
681+
NPY_MAX_INT, narrays_true);
682+
return NULL;
683+
}
684+
narrays = (int)narrays_true;
678685
arrays = PyArray_malloc(narrays * sizeof(arrays[0]));
679686
if (arrays == NULL) {
680687
PyErr_NoMemory();

numpy/_core/tests/test_shape_base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
13
import pytest
24

35
import numpy as np
@@ -29,6 +31,7 @@
2931
assert_raises,
3032
assert_raises_regex,
3133
)
34+
from numpy.testing._private.utils import requires_memory
3235

3336

3437
class TestAtleast1d:
@@ -290,6 +293,17 @@ def test_exceptions(self):
290293
# No arrays to concatenate raises ValueError
291294
assert_raises(ValueError, concatenate, ())
292295

296+
@pytest.mark.slow
297+
@pytest.mark.skipif(sys.maxsize < 2**32, reason="only problematic on 64bit platforms")
298+
@requires_memory(2 * np.iinfo(np.intc).max)
299+
def test_huge_list_error(self):
300+
a = np.array([1])
301+
max_int = np.iinfo(np.intc).max
302+
arrs = (a,) * (max_int + 1)
303+
msg = fr"concatenate\(\) only supports up to {max_int} arrays but got {max_int + 1}."
304+
with pytest.raises(ValueError, match=msg):
305+
np.concatenate(arrs)
306+
293307
def test_concatenate_axis_None(self):
294308
a = np.arange(4, dtype=np.float64).reshape((2, 2))
295309
b = list(range(3))

0 commit comments

Comments
 (0)