fix(stringbuilder): guard capacity growth against Int overflow - #3837
Open
mizchi wants to merge 1 commit into
Open
fix(stringbuilder): guard capacity growth against Int overflow#3837mizchi wants to merge 1 commit into
mizchi wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens the non-JS StringBuilder (UTF-16 buffer-backed implementation) against Int overflow during capacity doubling, preventing potential non-terminating growth loops for very large required sizes.
Changes:
- Introduces a private
stringbuilder_grow_capacity(current, required)helper that detects overflow during doubling and falls back to allocating exactlyrequired. - Updates
StringBuilder::grow_if_necessaryto use the new helper instead of an unguarded doubling loop. - Adds allocation-free whitebox tests covering normal doubling behavior and overflow boundary cases, plus a focused benchmark for growth from the minimum capacity.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| builtin/stringbuilder_buffer.mbt | Adds overflow-safe capacity growth helper and wires it into grow_if_necessary. |
| builtin/stringbuilder_grow_wbtest.mbt | Adds whitebox tests validating normal doubling and overflow survival without allocating. |
| builtin/stringbuilder_bench_test.mbt | Adds a benchmark targeting growth behavior starting from minimal initial capacity. |
| builtin/moon.pkg | Excludes the new whitebox test from JS targets to match the non-JS implementation selection. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
mizchi
force-pushed
the
fix/stringbuilder-grow-overflow
branch
from
July 27, 2026 09:37
545c6ce to
05f4de3
Compare
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.
Summary
StringBuildercapacity growth againstIntoverflow.Root cause
StringBuilder::grow_if_necessaryrepeatedly doubled the backing capacity. Once the capacity exceeded2^30,Intoverflow could make the loop cycle through non-positive values and never reach the requested length. The new private helper falls back to the exact requested capacity after an overflowing double.This is the
StringBuildercounterpart to #3822, but it changes the separate UTF-16 builder implementation.Validation
git diff --check upstream/main...HEADmoon test -p builtin --target all --no-renderpassed in a local compatibility environment (wasm: 2887, wasm-gc: 2887, js: 2873, native: 2845).The installed compiler currently rejects an unrelated pattern in
builtin/iterator.mbt:789on an unmodified upstream checkout; that is why the target test command needs the local compatibility environment above.