Skip to content

fix: reject overflowing shapes, count huge-step slices, broadcast 0-d views - #8

Merged
tannevaled merged 1 commit into
mainfrom
fix/shape-index-overflow
Oct 4, 2026
Merged

tannevaled merged 1 commit into
mainfrom
fix/shape-index-overflow

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Three defects found while probing edge cases; each has a test that fails without its fix (each fix was reverted in turn to check).

Element-count overflow. prod() multiplied dimensions without a guard:

call before after (numpy: ValueError "array is too big")
New(1<<62, 4) array with Size() == 0 ErrShapeMismatch
FromData(4 elems, 1<<62+1, 4) accepted (product wraps to 4); At(1, 0) panics ErrShapeMismatch
Reshape(1<<62+1, 4) on a size-4 array accepted ErrShapeMismatch
New(1<<59, 8) panic in makeslice ErrShapeMismatch
(2^40, 1) + (1, 2^40) wraps to an empty result ErrShapeMismatch

validateShape now rejects a shape whose float64 storage would not fit in an int; Reshape and broadcastShape call it too.

Slice with a huge step. ceil((stop-start)/step) was computed as (stop-start+step-1)/step, which overflows: x[0:5:MaxInt] and x[::-MaxInt] returned empty views; numpy returns one element.

0-d views at an offset. x.Slice(A(2)) is a 0-d view with a non-zero offset, so it is not contiguous and goes through broadcastTo, which read shape[n-1] with n == 0: s.Add(s), MaskSelect, Where panicked with index out of range [-1].

🤖 Generated with Claude Code

… views

Three crashes or silent wrong results found while probing edge cases:

- Element counts were multiplied without an overflow check. New(1<<62, 4)
  returned an array with Size 0; FromData and Reshape accepted
  (1<<62+1, 4) for 4 elements because the product wrapped to exactly 4, and
  At(1, 0) then panicked; New(1<<59, 8) panicked in makeslice. Shapes whose
  float64 storage would not fit in an int are now rejected with
  ErrShapeMismatch, as numpy does ("array is too big"), including a
  broadcast result built from two valid shapes.
- Slice computed ceil((stop-start)/step) as (stop-start+step-1)/step, which
  overflows for a step near MaxInt: x[0:5:MaxInt] was empty instead of [x0].
- A 0-d view at a non-zero offset (x.Slice(A(2))) is not contiguous, so
  elementwise ops sent it down the broadcasting path, which read shape[n-1]
  with n == 0 and panicked with "index out of range [-1]".

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@tannevaled
tannevaled merged commit 6c7747d into main Oct 4, 2026
9 checks passed
@tannevaled
tannevaled deleted the fix/shape-index-overflow branch October 4, 2026 09:07
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