Skip to content

Commit 7b30ce7

Browse files
authored
BUG: Enforce integer limitation in concatenate (numpy#29231)
* BUG: Enforce integer limitation in concatenate Concatenate internals only deal with integer many arrays, that should be fine in practice, but a SystemError (or in principle maybe also a harder crash?) is not really. * skip 32bit systems
1 parent 4575abf commit 7b30ce7

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
@@ -669,10 +669,17 @@ PyArray_ConcatenateInto(PyObject *op,
669669
}
670670

671671
/* Convert the input list into arrays */
672-
narrays = PySequence_Size(op);
673-
if (narrays < 0) {
672+
Py_ssize_t narrays_true = PySequence_Size(op);
673+
if (narrays_true < 0) {
674674
return NULL;
675675
}
676+
else if (narrays_true > NPY_MAX_INT) {
677+
PyErr_Format(PyExc_ValueError,
678+
"concatenate() only supports up to %d arrays but got %zd.",
679+
NPY_MAX_INT, narrays_true);
680+
return NULL;
681+
}
682+
narrays = (int)narrays_true;
676683
arrays = PyArray_malloc(narrays * sizeof(arrays[0]));
677684
if (arrays == NULL) {
678685
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy