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 e897143

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

File tree

Expand file treeCollapse file tree

2 files changed

+29
-1
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+29
-1
lines changed

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

Copy file name to clipboardExpand all lines: numpy/_core/src/multiarray/ctors.c
+8-1Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2134,7 +2134,8 @@ PyArray_FromInterface(PyObject *origin)
21342134
PyArray_Descr *dtype = NULL;
21352135
char *data = NULL;
21362136
Py_buffer view;
2137-
int i, n;
2137+
Py_ssize_t i, n;
2138+
// int i, n;
21382139
npy_intp dims[NPY_MAXDIMS], strides[NPY_MAXDIMS];
21392140
int dataflags = NPY_ARRAY_BEHAVED;
21402141

@@ -2250,6 +2251,12 @@ PyArray_FromInterface(PyObject *origin)
22502251
/* Get dimensions from shape tuple */
22512252
else {
22522253
n = PyTuple_GET_SIZE(attr);
2254+
if (n > NPY_MAXDIMS) {
2255+
PyErr_Format(PyExc_ValueError,
2256+
"number of dimensions must be within [0, %d], got %d",
2257+
NPY_MAXDIMS, n);
2258+
goto fail;
2259+
}
22532260
for (i = 0; i < n; i++) {
22542261
PyObject *tmp = PyTuple_GET_ITEM(attr, i);
22552262
dims[i] = PyArray_PyIntAsIntp(tmp);

‎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.