Skip to content

fix(stringbuilder): guard capacity growth against Int overflow - #3837

Open
mizchi wants to merge 1 commit into
moonbitlang:mainfrom
mizchi:fix/stringbuilder-grow-overflow
Open

fix(stringbuilder): guard capacity growth against Int overflow#3837
mizchi wants to merge 1 commit into
moonbitlang:mainfrom
mizchi:fix/stringbuilder-grow-overflow

Conversation

@mizchi

@mizchi mizchi commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Guard StringBuilder capacity growth against Int overflow.
  • Add allocation-free whitebox coverage for normal growth and overflow boundaries.
  • Add a focused native growth benchmark.

Root cause

StringBuilder::grow_if_necessary repeatedly doubled the backing capacity. Once the capacity exceeded 2^30, Int overflow 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 StringBuilder counterpart to #3822, but it changes the separate UTF-16 builder implementation.

Validation

  • git diff --check upstream/main...HEAD
  • moon test -p builtin --target all --no-render passed in a local compatibility environment (wasm: 2887, wasm-gc: 2887, js: 2873, native: 2845).
  • Native release benchmark, alternating baseline/head runs: no clear measurable regression within run-to-run variance.

The installed compiler currently rejects an unrelated pattern in builtin/iterator.mbt:789 on an unmodified upstream checkout; that is why the target test command needs the local compatibility environment above.

@mizchi
mizchi marked this pull request as ready for review July 20, 2026 11:20
Copilot AI review requested due to automatic review settings July 20, 2026 11:20

Copilot AI 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.

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 exactly required.
  • Updates StringBuilder::grow_if_necessary to 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
mizchi force-pushed the fix/stringbuilder-grow-overflow branch from 545c6ce to 05f4de3 Compare July 27, 2026 09:37
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