Skip to content

Validate index_flat indices before casting to int32 - #40

Merged
dance858 merged 1 commit into
mainfrom
check-index-flat-before-cast
Nov 13, 2026
Merged

dance858 merged 1 commit into
mainfrom
check-index-flat-before-cast

Conversation

@dance858

Copy link
Copy Markdown
Collaborator

index_flat converted the indices with np.asarray(..., dtype=np.int32) before checking them. That conversion is unsafe, so bad input became a valid-looking index before the bounds check saw it:

x = s.Variable(3, 1)
sp.index_flat(x, np.array([2**32 + 1]), (1, 1))  # wrapped to index 1
sp.index_flat(x, [1.7], (1, 1))                  # truncated to index 1

An int32 array was also kept by reference. Editing it after the check reached the engine unchecked, which caused an out-of-bounds read at compile time.

The fix checks the dtype (integers only, so floats and bools are rejected) and the range on the input as given, then casts to int32, which also makes a copy. x[...] indexing isn't affected, since _axis_indices already validates first. It adds test_index_flat_checks_before_casting, which fails on main.

Part of #37 (earlier issue 1).

🤖 Generated with Claude Code

index_flat converted indices with np.asarray(..., dtype=np.int32) before
the bounds check, so 2**32 + 1 wrapped and 1.7 truncated to a valid index.
An int32 array was also kept by reference, so editing it after the check
reached the engine unchecked. Check dtype and range on the input, then
cast (which copies).

Part of #37.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dance858
dance858 merged commit b8665ed into main Nov 13, 2026
17 of 25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant