Skip to content

Avoid integer overflow in substring bounds - #907

Open
vinsymoyya wants to merge 2 commits into
google:masterfrom
vinsymoyya:fix/substr-large-bounds
Open

vinsymoyya wants to merge 2 commits into
google:masterfrom
vinsymoyya:fix/substr-large-bounds

Conversation

@vinsymoyya

@vinsymoyya vinsymoyya commented Nov 12, 2026 •

Copy link
Copy Markdown

Fixes #904.

A large nonnegative start such as std.substr("abc", 1e100, 1) is converted to a negative machine integer before the bounds check and produces an internal slice-bounds crash. Check the start against the rune count before conversion, and clamp the requested length to the remaining runes before computing the end index. This also prevents the end-index addition from overflowing on 32-bit platforms.

Regression coverage in builtins_bounds_test.go includes huge starts, empty input, a maximum accepted length, and Unicode input. The file is registered in the Bazel test target. The two huge-start cases fail on the original code with internal slice-bounds errors and pass with the fix.

Validation:

  • Go 1.27.4 Windows/amd64 and Windows/386: go test . -run TestSubstrLargeBounds -count=1 passes (with GOARCH=386 for the 32-bit run).
  • Go 1.24.13 Linux/amd64, from an immutable archive of this commit: go test -buildvcs=false ./... passes.
  • gofmt and git diff --check pass.

The native Windows broad run encounters pre-existing golden-path separator mismatches (testdata\ versus testdata/); the full Go suite passes in Linux. Bazel and the C/Python-binding integration suite have not been run locally.

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.

std.substr crashes with a large nonnegative start or overflowing bounds

1 participant