Skip to content

fix: prevent stack overflow in toBase64 for large inputs#961

Open
Scottcjn wants to merge 1 commit intoanthropics:mainfrom
Scottcjn:fix/tobase64-stack-overflow
Open

fix: prevent stack overflow in toBase64 for large inputs#961
Scottcjn wants to merge 1 commit intoanthropics:mainfrom
Scottcjn:fix/tobase64-stack-overflow

Conversation

@Scottcjn
Copy link
Copy Markdown

Summary

  • toBase64 uses String.fromCharCode.apply(null, data) in the btoa path, which passes the entire Uint8Array as individual arguments. JavaScript engines have a maximum argument count (~65,536), so inputs larger than ~100KB cause RangeError: Maximum call stack size exceeded.
  • Fix: process bytes in 8,192-byte chunks via String.fromCharCode(...data.subarray(i, i + chunkSize)), keeping each call well under the limit.
  • Added a test that round-trips a 200KB Uint8Array through toBase64/fromBase64 in both Buffer and btoa modes.

Fixes #932

Test plan

  • Existing toBase64 and fromBase64 tests pass (both Buffer and atob modes)
  • New 200KB round-trip test passes in both modes
  • CI green

🤖 Generated with Claude Code

String.fromCharCode.apply() has a maximum argument count limit (~65536)
which causes "Maximum call stack size exceeded" when encoding Uint8Arrays
larger than ~100KB. Process bytes in 8192-byte chunks instead.

Fixes anthropics#932

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@Scottcjn Scottcjn requested a review from a team as a code owner March 26, 2026 15:21
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.

toBase64 stack overflow on large inputs (e.g. image uploads) in non-Node environments

1 participant