Repository navigation
fix: reject overflowing shapes, count huge-step slices, broadcast 0-d views - #8
Merged
Merged
Conversation
… 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>
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.
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:New(1<<62, 4)Size() == 0ErrShapeMismatchFromData(4 elems, 1<<62+1, 4)At(1, 0)panicsErrShapeMismatchReshape(1<<62+1, 4)on a size-4 arrayErrShapeMismatchNew(1<<59, 8)makesliceErrShapeMismatch(2^40, 1) + (1, 2^40)ErrShapeMismatchvalidateShapenow rejects a shape whose float64 storage would not fit in anint;ReshapeandbroadcastShapecall 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]andx[::-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 throughbroadcastTo, which readshape[n-1]withn == 0:s.Add(s),MaskSelect,Wherepanicked withindex out of range [-1].🤖 Generated with Claude Code