nodejs/test/parallel/test-buffer-pool-untransferable.js
Anna Henningsen bb5e7097e8
buffer: mark pool ArrayBuffer as untransferable
This removes a footgun in which users could attempt to transfer the
pooled ArrayBuffer underlying a regular `Buffer`, which would lead to
all `Buffer`s that share the same pool being rendered unusable as well,
and potentially break creation of new pooled `Buffer`s.

This disables this kind of transfer.

Refs: https://github.com/nodejs/node/issues/32752

PR-URL: https://github.com/nodejs/node/pull/32759
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Richard Lau <riclau@uk.ibm.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: James M Snell <jasnell@gmail.com>
2020-04-13 18:47:27 +02:00

21 lines
633 B
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const { MessageChannel } = require('worker_threads');
// Make sure that the pools used by the Buffer implementation are not
// transferable.
// Refs: https://github.com/nodejs/node/issues/32752
const a = Buffer.from('hello world');
const b = Buffer.from('hello world');
assert.strictEqual(a.buffer, b.buffer);
const length = a.length;
const { port1 } = new MessageChannel();
port1.postMessage(a, [ a.buffer ]);
// Verify that the pool ArrayBuffer has not actually been transfered:
assert.strictEqual(a.buffer, b.buffer);
assert.strictEqual(a.length, length);