Repository navigation
fix(it): prevent Subset end bound overflow - #1056
Open
Alifarid0011 wants to merge 1 commit into
Open
Alifarid0011 wants to merge 1 commit into
Alifarid0011 wants to merge 1 commit into
Conversation
Clamp the requested length before adding the offset so large lengths return the remaining sequence instead of an empty result.
agammann
approved these changes
Nov 1, 2026
agammann
left a comment
Contributor
There was a problem hiding this comment.
I checked the bound calculation for valid nonnegative offsets: capping length before offset+length keeps the end representable, so Subset returns the remaining items instead of an empty iterator. Negative offsets still take the existing panic path before this calculation. The focused Subset table passes on this head, including maximum and one-overflow cases.
This branch has not been deployed
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.
it.Subset(slices.Values([]int{0, 1, 2, 3, 4}), 2, math.MaxInt)currently yields no values instead of2, 3, 4:offset + lengthoverflows to a negative end bound, whichSliceclamps to zero.Cap the requested length at
math.MaxInt - offsetafter the existing negative-argument checks, so the exclusive end remains representable. This preserves the API and ordinary subset behavior without allocating or consuming input during construction.Extend the existing table test with a maximum length at zero and positive offsets, an addition overflowing by one, and a maximum offset/length pair. The two positive-offset output regressions fail on unchanged upstream
5c6ddcbon both Linux amd64 and 386, and pass with the guard.Validation with Go 1.26.8:
go test ./it -run '^TestSubset$' -count=1GOARCH=386 go test ./it -run '^TestSubset$' -count=1(native execution)go test ./... -count=1CGO_ENABLED=1 CC=gcc make test(full race suite)make build,go vet ./..., and golangci-lint v2.13.1: 0 issuesSubsetis 100% covered; gofmt and diff checks are clean.The separate Go 1.27 experimental SIMD module was not tested. Related #1009 fixes
Sliceinput overconsumption; this change only prevents overflow inSubsetbefore calling it. #687 changes the root slice API and does not cover the iterator helper.Prepared with AI assistance; the baseline failure and the checks above were executed for this change.