Skip to content

Fix crash on ellipsis indexing with too many trailing indices - #4396

Open
Adityaj0 wants to merge 4 commits into
ml-explore:mainfrom
Adityaj0:pr-fix-ellipsis-indexing-crash
Open

Fix crash on ellipsis indexing with too many trailing indices#4396
Adityaj0 wants to merge 4 commits into
ml-explore:mainfrom
Adityaj0:pr-fix-ellipsis-indexing-crash

Conversation

@Adityaj0

@Adityaj0 Adityaj0 commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #4398

Indexing with an ellipsis followed by more non-None indices than the array has dimensions computed an unsigned (size_t) underflow in the ellipsis-expansion loop bound, causing an out-of-bounds read / crash instead of raising a ValueError. Adds a regression test covering getitem and setitem.

mlx_expand_ellipsis in python/src/indexing.cpp computed the ellipsis
expansion loop bound as `shape.size() - non_none_indices_after`, mixing
an unsigned size_t with a signed int. When an index expression has more
non-None indices after the ellipsis than the array has dimensions (e.g.
a[..., 0, 0] on a 1-D array), this subtraction underflows to a huge
unsigned value, so the loop reads far past the end of the shape vector
and pushes an unbounded number of slices, crashing the process (SIGSEGV)
instead of raising an error. The existing "too many indices" check in
mlx_get_item_nd ran only after this expansion, so it never caught this
case.

Move the bounds check before expansion and do the arithmetic in signed
int, so this now raises the same "Too many indices" ValueError that
already applies when the extra indices are before the ellipsis.

Adds a regression test in python/tests/test_array.py covering
getitem and setitem with an ellipsis followed by too many indices.
@nastya236
nastya236 self-requested a review August 25, 2026 11:22

@nastya236 nastya236 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the fix! I merged the test into indexing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Crash on ellipsis indexing with too many trailing indices

2 participants