Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ function createPool() {
}
createPool();

function ensurePool() {
// Recreate the pool if it was detached (e.g., via ArrayBuffer.prototype.transfer).
if (allocPool.byteLength === 0)
createPool();
}

function alignPool() {
// Ensure aligned slices
if (poolOffset & 0x7) {
Expand Down Expand Up @@ -436,6 +442,7 @@ function allocate(size) {
return new FastBuffer();
}
if (size < (Buffer.poolSize >>> 1)) {
ensurePool();
if (size > (poolSize - poolOffset))
createPool();
const b = new FastBuffer(allocPool, poolOffset, size);
Expand All @@ -462,6 +469,7 @@ function fromStringFast(string, ops) {
if (length >= maxLength)
return createFromString(string, ops, length);

ensurePool();
if (length > (poolSize - poolOffset))
createPool();

Expand Down Expand Up @@ -525,6 +533,7 @@ function fromArrayLike(obj) {
if (obj.length <= 0)
return new FastBuffer();
if (obj.length < (Buffer.poolSize >>> 1)) {
ensurePool();
if (obj.length > (poolSize - poolOffset))
createPool();
const b = new FastBuffer(allocPool, poolOffset, obj.length);
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-buffer-pool-untransferable.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,16 @@ assert.throws(() => port1.postMessage(a, [ a.buffer ]), {
// Verify that the pool ArrayBuffer has not actually been transferred:
assert.strictEqual(a.buffer, b.buffer);
assert.strictEqual(a.length, length);

if (typeof ArrayBuffer.prototype.transfer !== 'function')
common.skip('ArrayBuffer.prototype.transfer is not available');

// Regression test for https://github.com/nodejs/node/issues/61362
const base64 = 'aGVsbG8='; // "hello"
const buf = Buffer.from(base64, 'base64');
buf.buffer.transfer();
assert.strictEqual(buf.buffer.byteLength, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The buffer should not be transferrable at all... This still transfers the underlying buffer.

assert.doesNotThrow(() => {
const out = Buffer.from(base64, 'base64');
assert.strictEqual(out.toString(), 'hello');
});