Fix crash on ellipsis indexing with too many trailing indices - #4396
Open
Adityaj0 wants to merge 4 commits into
Open
Fix crash on ellipsis indexing with too many trailing indices#4396Adityaj0 wants to merge 4 commits into
Adityaj0 wants to merge 4 commits into
Conversation
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
self-requested a review
August 25, 2026 11:22
nastya236
approved these changes
Aug 25, 2026
nastya236
left a comment
Collaborator
There was a problem hiding this comment.
thanks for the fix! I merged the test into indexing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.