Skip to content

src: fix SAB race in transcode and StringDecoder - #67647

Open
christianaurichzm wants to merge 1 commit into
nodejs:mainfrom
christianaurichzm:sab-race-transcode-decoder
Open

christianaurichzm wants to merge 1 commit into
nodejs:mainfrom
christianaurichzm:sab-race-transcode-decoder

Conversation

@christianaurichzm

Copy link
Copy Markdown
Member

StringDecoder and buffer.transcode() can write past the end of their output if another thread changes the SharedArrayBuffer they're reading.

const { StringDecoder } = require('node:string_decoder');
const { Worker } = require('node:worker_threads');

const sab = new SharedArrayBuffer(2 * 1024 * 1024);
new Worker(`
  const buf = Buffer.from(require('node:worker_threads').workerData);
  for (;;) { buf.fill('A\\0'); buf.fill('é'); }
`, { eval: true, workerData: sab }).on('online', () => {
  for (let i = 0; i < 100; i++) new StringDecoder('utf8').write(Buffer.from(sab));
  process.exit();
});
$ ./node repro.js

  #  ./node[141074]: node::StringBytes::Encode(v8::Isolate*, const char*, size_t, node::encoding)::<lambda(uint16_t*)> at ../src/string_bytes.cc:690
  #  Assertion failed: (written) == (u16size)

Both read the memory in place, and their UTF-8/UTF-16 conversions size the output in one pass and fill it in another. StringDecoder gets there through StringBytes::Encode() for UTF-8 input over 1 MiB, where the CHECK_EQ above only runs after the write. transcode() between UTF-8 and UTF-16LE has no check and ends in a SIGSEGV or a corrupted heap. This copies shared input first, as #63517 did for StringSlice() and DecodeUTF8().

Refs: #63517

buffer.transcode() and StringDecoder read SharedArrayBuffer-backed
input in place, and their UTF-8/UTF-16 conversions size the output in
one pass and fill it in another. A write from another thread in
between makes them write past the end of the output. Copy shared input
first, as StringSlice() and DecodeUTF8() already do.

Signed-off-by: Christian Aurich Zanettini Martins <christian.aurichzm@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++. i18n-api Issues and PRs related to Node.js internationalization support. needs-ci PRs that need a full CI run. labels Nov 20, 2026
@codecov

codecov Bot commented Nov 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 87.09677% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.72%. Comparing base (cdc1dac) to head (bfeb18f).
⚠️ Report is 91 commits behind head on main.

Files with missing lines Patch % Lines
src/node_i18n.cc 89.47% 0 Missing and 2 partials ⚠️
src/string_decoder.cc 83.33% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67647      +/-   ##
==========================================
+ Coverage   90.67%   90.72%   +0.05%     
==========================================
  Files         800      801       +1     
  Lines      280932   280932              
  Branches    54285    54268      -17     
==========================================
+ Hits       254731   254876     +145     
+ Misses      16309    16247      -62     
+ Partials     9892     9809      -83     
Files with missing lines Coverage Δ
src/node_i18n.cc 78.72% <89.47%> (+0.45%) ⬆️
src/string_decoder.cc 90.95% <83.33%> (-1.23%) ⬇️

... and 97 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++. i18n-api Issues and PRs related to Node.js internationalization support. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants