Skip to content

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

Closed

Description

@Adityaj0

Describe the bug

Indexing an array with an ellipsis followed by more non-None indices than the array has dimensions crashes instead of raising a ValueError. mlx_expand_ellipsis in python/src/indexing.cpp computes the ellipsis-expansion loop bound as shape.size() - non_none_indices_after, where shape.size() is unsigned. When non_none_indices_after > shape.size(), this underflows, producing a huge loop bound and an out-of-bounds read / crash.

To Reproduce

import mlx.core as mx
a = mx.array([1, 2, 3])
a[..., 0, 0]  # crashes instead of raising ValueError

Expected behavior
Should raise ValueError (too many indices for array), matching NumPy's behavior, instead of crashing.

Fix
Fix + regression test up in #4396.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions