Skip to content

fix(it): prevent Subset end bound overflow - #1056

Open
Alifarid0011 wants to merge 1 commit into
samber:masterfrom
Alifarid0011:fix/iterator-subset-overflow
Open

Alifarid0011 wants to merge 1 commit into
samber:masterfrom
Alifarid0011:fix/iterator-subset-overflow

Conversation

@Alifarid0011

Copy link
Copy Markdown

it.Subset(slices.Values([]int{0, 1, 2, 3, 4}), 2, math.MaxInt) currently yields no values instead of 2, 3, 4: offset + length overflows to a negative end bound, which Slice clamps to zero.

Cap the requested length at math.MaxInt - offset after 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 5c6ddcb on both Linux amd64 and 386, and pass with the guard.

Validation with Go 1.26.8:

  • go test ./it -run '^TestSubset$' -count=1
  • GOARCH=386 go test ./it -run '^TestSubset$' -count=1 (native execution)
  • go test ./... -count=1
  • CGO_ENABLED=1 CC=gcc make test (full race suite)
  • make build, go vet ./..., and golangci-lint v2.13.1: 0 issues
  • Focused coverage: Subset is 100% covered; gofmt and diff checks are clean.

The separate Go 1.27 experimental SIMD module was not tested. Related #1009 fixes Slice input overconsumption; this change only prevents overflow in Subset before 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.

Clamp the requested length before adding the offset so large lengths return the remaining sequence instead of an empty result.

@agammann agammann left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

No deployments
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.

2 participants