Skip to content

buffer: throw on write() to immutable buffers - #67741

Open
vedchaudhari wants to merge 1 commit into
nodejs:mainfrom
vedchaudhari:buffer-write-immutable
Open

vedchaudhari wants to merge 1 commit into
nodejs:mainfrom
vedchaudhari:buffer-write-immutable

Conversation

@vedchaudhari

Copy link
Copy Markdown
Contributor

buf.write() and the encoding-specific write methods (utf8Write(), hexWrite(), etc.) write into buffers backed by an immutable ArrayBuffer:

// node --js-immutable-arraybuffer
const buf = Buffer.from(new Uint8Array([1, 2, 3, 4]).buffer.transferToImmutable());
buf.write('hi');
console.log(buf); // <Buffer 68 69 03 04>

buf.fill(), buf.set() and the writeUInt*() methods already throw here.

This adds the check to the three native write paths, including the fast API one, and throws ERR_INVALID_ARG_VALUE like crypto.randomFillSync() does (#66944). Invalid arguments are still reported first, and zero-byte writes throw too, like TypedArray methods.

SPREAD_BUFFER_ARG now keeps the ArrayBuffer it already fetches, so the check doesn't need a second Buffer() call. With that, the buffers/buffer-write-string* benchmarks are about -1% on average, and at most about -3.5% for 1-32 byte writes.

Refs: #66346

Assisted-by: an AI coding agent. Reviewed and tested locally by me.

cc @jasnell @panva

buf.write() could change the bytes of a buffer backed by an
immutable ArrayBuffer. Throw ERR_INVALID_ARG_VALUE instead, like
other APIs already do.

Refs: nodejs#66346
Signed-off-by: vedchaudhari <vedc2853@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 Nov 24, 2026
@codecov

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.52%. Comparing base (d2c9ac0) to head (2053ed6).

Files with missing lines Patch % Lines
src/node_buffer.cc 83.33% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67741      +/-   ##
==========================================
- Coverage   90.54%   90.52%   -0.02%     
==========================================
  Files         803      803              
  Lines      285094   285106      +12     
  Branches    55024    55032       +8     
==========================================
- Hits       258133   258098      -35     
- Misses      17105    17140      +35     
- Partials     9856     9868      +12     
Files with missing lines Coverage Δ
src/util.h 90.84% <ø> (ø)
src/node_buffer.cc 75.51% <83.33%> (+0.22%) ⬆️

... and 39 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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