Skip to content

buffer: avoid 32-bit truncation in copy beyond 2 GiB#62942

Open
maruthang wants to merge 1 commit intonodejs:mainfrom
maruthang:fix-55422-buffer-copy-overflow
Open

buffer: avoid 32-bit truncation in copy beyond 2 GiB#62942
maruthang wants to merge 1 commit intonodejs:mainfrom
maruthang:fix-55422-buffer-copy-overflow

Conversation

@maruthang
Copy link
Copy Markdown

Buffer.prototype.copy and copyArrayBuffer use Uint32Value() to
read offset and length arguments in C++. Values larger than 2**32
are silently truncated, so a 4 GiB+ copy quietly drops bytes.

Switch to IntegerValue() and widen internal types to size_t so the
slow path handles the full kMaxLength range. The fast path keeps
its uint32_t signature; values beyond UINT32_MAX naturally fall
through to the slow path.

Add a regression test gated on NODE_TEST_LARGE_BUFFER (matches
test-buffer-tostring-4gb.js gating) so default CI does not need
to allocate >4 GiB.

Note: I was unable to run a full local build on Windows (vcbuild.bat fails due to missing NASM and inaccessible WindowsApps Python). The patch mirrors the diagnosis posted by the issue reporter. cpplint is clean. Looking forward to CI verification.

Fixes: #55422

`Buffer.prototype.copy` and `copyArrayBuffer` use Uint32Value() to
read offset and length arguments in C++. Values larger than 2**32
are silently truncated, so a 4 GiB+ copy quietly drops bytes.

Switch to IntegerValue() and widen internal types to size_t so the
slow path handles the full kMaxLength range. The fast path keeps
its uint32_t signature; values beyond UINT32_MAX naturally fall
through to the slow path.

Add a regression test gated on NODE_TEST_LARGE_BUFFER (matches
test-buffer-tostring-4gb.js gating) so default CI does not need
to allocate >4 GiB.

Fixes: nodejs#55422
Signed-off-by: Maruthan G <maruthang4@gmail.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 Apr 25, 2026
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.

Buffer.concat and Buffer.copy silently produce invalid results when the operation involves indices equal or greater than 2^32

2 participants