Skip to content

buffer: support buffers over 4 GiB in string writes - #67987

Open
rajendra0000 wants to merge 2 commits into
nodejs:mainfrom
rajendra0000:fix-write-string-4gib
Open

rajendra0000 wants to merge 2 commits into
nodejs:mainfrom
rajendra0000:fix-write-string-4gib

Conversation

@rajendra0000

@rajendra0000 rajendra0000 commented Dec 3, 2026 •

Copy link
Copy Markdown

Writing a string into a Buffer bigger than 4 GiB crashes the process - but only once V8 optimizes the call and switches to the fast API path. Reading into a BYOB view at an offset past 4 GiB crashes the same way.

Reason: the bindings take offset and length as uint32_t. On the fast path V8 wraps a larger double into uint32_t, so the offset wraps and CHECK(dst_length - offset <= UINT32_MAX) kills the process. Before optimization the same call returns fine, which is why it only shows up after a lot of iterations. Just removing the CHECK would be worse: the write would silently land at the wrong address.

Repro:

const big = Buffer.alloc(2 ** 32 + 4096);
for (let i = 0; i < 300_000; i++) big.write('abc', i & 1023, 'latin1');

Aborts on current main with Assertion failed: dst_length - offset <= std::numeric_limits<uint32_t>::max().

The fix passes offsets and lengths as doubles across the fast/slow API boundary and uses size_t internally - the same pattern #63828 used for Buffer.prototype.copy:

  • FastWriteString and WriteOneByteString take double offset and max_length, size_t inside
  • CopyArrayBuffer and FastCopyArrayBuffer take doubles, converted with the existing CheckNumberToSize

Tests go in test/pummel since they need >4 GiB allocations; they skip on 32-bit and on allocation failure:

  • test-buffer-large-size-write-string.js: hot-loop writes for utf8, latin1 and ascii, offsets both below and above 4 GiB, checks the bytes land at the right index
  • test-webstream-byob-large-offset.js: reads a byte stream into a view at offset 2 ** 32 + 8

Build and tests: Linux x64, gcc, --error-on-warn, running in CI in my fork. Before it, both tests abort. After it, both pass, along with parallel/test-buffer-, parallel/test-whatwg-readablebytestream, parallel/test-stream-* and pummel/test-buffer-* (325 tests). Logs: https://github.com/rajendra0000/node/actions/runs/47101442812 (fix) and https://github.com/rajendra0000/node/actions/runs/47101441759 (tests only, aborting).

I used an AI coding assistant to help investigate and draft this change. The builds and tests above ran in CI in my own fork, and I reviewed the results before submitting.

Refs: #55422

Buffer.prototype.write() on a Buffer larger than 4 GiB aborts the
process once V8 calls the fast API variant of the one-byte string
writers, because offset and max_length are passed as uint32_t. Reading
into a BYOB view at an offset above 4 GiB fails a CHECK in the native
ArrayBuffer copy for the same reason.

Signed-off-by: Rajendra Singh Asoliya <121512381+rajendra0000@users.noreply.github.com>
The fast API variants of asciiWrite, latin1Write and utf8Write, and of
the ArrayBuffer copy binding used by web streams, took offsets and
lengths as uint32_t. V8 wraps larger values, so writing into a Buffer
larger than 4 GiB either aborted the process on
CHECK(dst_length - offset <= UINT32_MAX) or targeted a wrapped offset.

Pass offsets and lengths as doubles and use size_t internally, as
buffer.copy() already does.

Refs: nodejs#55422
Signed-off-by: Rajendra Singh Asoliya <121512381+rajendra0000@users.noreply.github.com>
@nodejs-github-bot nodejs-github-bot added buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants