Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bc00011

Browse filesBrowse files
committed
BUG: Prevent segmentation fault in np.asanyarray
1 parent ed8160f commit bc00011
Copy full SHA for bc00011

File tree

Expand file treeCollapse file tree

2 files changed

+42
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+42
-2
lines changed

‎numpy/_core/src/multiarray/ctors.c

Copy file name to clipboardExpand all lines: numpy/_core/src/multiarray/ctors.c
+21-2Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2249,14 +2249,21 @@ PyArray_FromInterface(PyObject *origin)
22492249
}
22502250
/* Get dimensions from shape tuple */
22512251
else {
2252-
n = PyTuple_GET_SIZE(attr);
2253-
for (i = 0; i < n; i++) {
2252+
Py_ssize_t shape_tuple_len = PyTuple_GET_SIZE(attr);
2253+
if (shape_tuple_len > NPY_MAXDIMS) {
2254+
PyErr_Format(PyExc_ValueError,
2255+
"number of dimensions must be within [0, %d], got %d",
2256+
NPY_MAXDIMS, shape_tuple_len);
2257+
goto fail;
2258+
}
2259+
for (i = 0; i < shape_tuple_len; i++) {
22542260
PyObject *tmp = PyTuple_GET_ITEM(attr, i);
22552261
dims[i] = PyArray_PyIntAsIntp(tmp);
22562262
if (error_converting(dims[i])) {
22572263
goto fail;
22582264
}
22592265
}
2266+
n = (int)shape_tuple_len;
22602267
}
22612268
Py_CLEAR(attr);
22622269

@@ -4140,6 +4147,12 @@ _array_fill_strides(npy_intp *strides, npy_intp const *dims, int nd, size_t item
41404147
for (i = 0; i < nd; i++) {
41414148
strides[i] = itemsize;
41424149
if (dims[i]) {
4150+
npy_intp tmp;
4151+
if (npy_mul_sizes_with_overflow(&tmp, (npy_intp)itemsize, dims[i])) {
4152+
PyErr_SetString(PyExc_ValueError,
4153+
"array is too big; overflow in _array_fill_strides.");
4154+
return;
4155+
}
41434156
itemsize *= dims[i];
41444157
}
41454158
else {
@@ -4158,6 +4171,12 @@ _array_fill_strides(npy_intp *strides, npy_intp const *dims, int nd, size_t item
41584171
for (i = nd - 1; i >= 0; i--) {
41594172
strides[i] = itemsize;
41604173
if (dims[i]) {
4174+
npy_intp tmp;
4175+
if (npy_mul_sizes_with_overflow(&tmp, (npy_intp)itemsize, dims[i])) {
4176+
PyErr_SetString(PyExc_ValueError,
4177+
"array is too big; overflow in _array_fill_strides.");
4178+
return;
4179+
}
41614180
itemsize *= dims[i];
41624181
}
41634182
else {

‎numpy/_core/tests/test_multiarray.py

Copy file name to clipboardExpand all lines: numpy/_core/tests/test_multiarray.py
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10420,3 +10420,24 @@ def test_to_device(self):
1042010420
r"The stream argument in to_device\(\) is not supported"
1042110421
):
1042210422
arr.to_device("cpu", stream=1)
10423+
10424+
def test_array_interface_excess_dimensions_raises():
10425+
"""Regression test for gh-27949: ensure too many dims raises ValueError instead of segfault."""
10426+
10427+
# Dummy object to hold a custom __array_interface__
10428+
class DummyArray:
10429+
def __init__(self, interface):
10430+
# Attach the array interface dict to mimic an array
10431+
self.__array_interface__ = interface
10432+
10433+
# Create a base array (scalar) and copy its interface
10434+
base = np.array(42) # base can be any scalar or array
10435+
interface = dict(base.__array_interface__)
10436+
10437+
# Modify the shape to exceed NumPy's dimension limit (NPY_MAXDIMS, typically 64)
10438+
interface['shape'] = tuple([1] * 136) # match the original bug report
10439+
10440+
dummy = DummyArray(interface)
10441+
# Now, using np.asanyarray on this dummy should trigger a ValueError (not segfault)
10442+
with pytest.raises(ValueError, match="dimensions must be within"):
10443+
np.asanyarray(dummy)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.