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

@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.50%. Comparing base (fa49b13) to head (36f339b).
⚠️ Report is 1 commits behind head on main.

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   92.59%   90.50%   -2.10%     
==========================================
  Files         433      805     +372     
  Lines      200382   285818   +85436     
  Branches    30786    55189   +24403     
==========================================
+ Hits       185553   258685   +73132     
- Misses      14486    17219    +2733     
- Partials      343     9914    +9571     
Files with missing lines Coverage Δ
src/util.h 90.84% <ø> (ø)
src/node_buffer.cc 75.51% <83.33%> (ø)

... and 514 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.

@addaleax

Copy link
Copy Markdown
Member

You'll probably want to rebase this after #67733 lands

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>
@vedchaudhari
vedchaudhari force-pushed the buffer-write-immutable branch from 2053ed6 to 36f339b Compare November 26, 2026 08:44
@vedchaudhari

Copy link
Copy Markdown
Contributor Author

You'll probably want to rebase this after #67733 lands

Rebased on top of #67733.

@vedchaudhari

Copy link
Copy Markdown
Contributor Author

Could someone start CI? Thanks!

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.

4 participants